Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Removed the HTTP layer and started to use HTTPlug #641

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Starting with version 5, the Facebook PHP SDK follows [SemVer](http://semver.org
- Replace custom CSPRNG implementation with `paragonie/random_compat` (#644)
- Removed the built-in autoloader in favor of composer's autoloader (#646)
- Big integers in signed requests get decoded as `string` instead of `float` (#699)
- We use an HTTP client abstraction called HTTPlug to give the user more control over *how* to send PSR7 messages. See updated installation instructions.
- Removed option `http_client_handler`
- Added option `http_client` which should be an object implementing `\Http\Client\HttpClient`
- Removed functions `FacebookClient::setHttpClientHandler()` and `FacebookClient::getHttpClientHandler()` in favor for `FacebookClient::getHttpClient()` and `FacebookClient::setHttpClient()`.

## 5.x

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ This repository contains the open source PHP SDK that allows you to access the F
The Facebook PHP SDK can be installed with [Composer](https://getcomposer.org/). Run this command:

```sh
composer require facebook/graph-sdk
composer require facebook/graph-sdk php-http/curl-client guzzlehttp/psr7
```

Why the extra packages? We give you the flexibility to choose what HTTP client (e.g. cURL or Guzzle) to use and what PSR-7 implementation you prefer. Read more about this at the [HTTPlug documentation](http://php-http.readthedocs.io/en/latest/httplug/users.html).


## Usage

Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
"sort-packages": true
},
"require": {
"php": "^7.1"
"php": "^7.1",
"psr/http-message": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/httplug": "^1.0",
"php-http/discovery": "^1.0",

"php-http/message": "^1.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^5.3.1",
"phpunit/phpunit": "^6.2"
},
"suggest": {
"guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
"phpunit/phpunit": "^6.2",
"php-http/guzzle6-adapter": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/FacebookVideo.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $myVideoFileToUpload = $fb->videoToUpload('/path/to/video-file.mp4'),

Partial file uploads are possible using the `$maxLength` and `$offset` parameters which provide the same functionality as the `$maxlen` and `$offset` parameters on the [`stream_get_contents()` PHP function](http://php.net/stream_get_contents).

> **Warning:** Uploading videos may cause a timeout. Make sure to configure your HTTP client to increase timeout time before uploading videos.

## Usage

In Graph v2.3, functionality was added to [upload video files in chunks](https://developers.facebook.com/docs/graph-api/video-uploads#resumable). The PHP SDK provides a handy API to easily upload video files in chunks via the [`uploadVideo()` method](Facebook.md#uploadvideo).
Expand Down
9 changes: 6 additions & 3 deletions src/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
use Facebook\GraphNodes\GraphEdge;
use Facebook\Url\UrlDetectionInterface;
use Facebook\Url\FacebookUrlDetectionHandler;
use Facebook\HttpClients\HttpClientsFactory;
use Facebook\PersistentData\PersistentDataFactory;
use Facebook\PersistentData\PersistentDataInterface;
use Facebook\Helpers\FacebookCanvasHelper;
use Facebook\Helpers\FacebookJavaScriptHelper;
use Facebook\Helpers\FacebookPageTabHelper;
use Facebook\Helpers\FacebookRedirectLoginHelper;
use Facebook\Exceptions\FacebookSDKException;
use Http\Client\HttpClient;

/**
* Class Facebook
Expand Down Expand Up @@ -117,7 +117,7 @@ public function __construct(array $config = [])
'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
'default_graph_version' => null,
'enable_beta_mode' => false,
'http_client_handler' => null,
'http_client' => null,
'persistent_data_handler' => null,
'url_detection_handler' => null,
], $config);
Expand All @@ -128,13 +128,16 @@ public function __construct(array $config = [])
if (!$config['app_secret']) {
throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
}
if ($config['http_client'] !== null && !$config['http_client'] instanceof HttpClient) {
throw new \InvalidArgumentException('Required "http_client" key to be null or an instance of \Http\Client\HttpClient');
}
if (!$config['default_graph_version']) {
throw new \InvalidArgumentException('Required "default_graph_version" key not supplied in config');
}

$this->app = new FacebookApp($config['app_id'], $config['app_secret']);
$this->client = new FacebookClient(
HttpClientsFactory::createHttpClient($config['http_client_handler']),
$config['http_client'],
$config['enable_beta_mode']
);
$this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler());
Expand Down
74 changes: 31 additions & 43 deletions src/FacebookClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
namespace Facebook;

use Facebook\HttpClients\FacebookHttpClientInterface;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookStreamHttpClient;
use Facebook\Exceptions\FacebookSDKException;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;

/**
* Class FacebookClient
Expand Down Expand Up @@ -76,9 +76,9 @@ class FacebookClient
protected $enableBetaMode = false;

/**
* @var FacebookHttpClientInterface HTTP client handler.
* @var HttpClient HTTP client handler.
*/
protected $httpClientHandler;
protected $httpClient;

/**
* @var int The number of calls that have been made to Graph.
Expand All @@ -88,43 +88,33 @@ class FacebookClient
/**
* Instantiates a new FacebookClient object.
*
* @param FacebookHttpClientInterface|null $httpClientHandler
* @param boolean $enableBeta
* @param HttpClient|null $httpClient
* @param boolean $enableBeta
*/
public function __construct(FacebookHttpClientInterface $httpClientHandler = null, $enableBeta = false)
public function __construct(HttpClient $httpClient = null, $enableBeta = false)
{
$this->httpClientHandler = $httpClientHandler ?: $this->detectHttpClientHandler();
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->enableBetaMode = $enableBeta;
}

/**
* Sets the HTTP client handler.
*
* @param FacebookHttpClientInterface $httpClientHandler
* @param HttpClient $httpClient
*/
public function setHttpClientHandler(FacebookHttpClientInterface $httpClientHandler)
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClientHandler = $httpClientHandler;
$this->httpClient = $httpClient;
}

/**
* Returns the HTTP client handler.
*
* @return FacebookHttpClientInterface
* @return HttpClient
*/
public function getHttpClientHandler()
public function getHttpClient()
{
return $this->httpClientHandler;
}

/**
* Detects which HTTP client handler to use.
*
* @return FacebookHttpClientInterface
*/
public function detectHttpClientHandler()
{
return extension_loaded('curl') ? new FacebookCurlHttpClient() : new FacebookStreamHttpClient();
return $this->httpClient;
}

/**
Expand Down Expand Up @@ -203,32 +193,30 @@ public function sendRequest(FacebookRequest $request)

list($url, $method, $headers, $body) = $this->prepareRequestMessage($request);

// Since file uploads can take a while, we need to give more time for uploads
$timeOut = static::DEFAULT_REQUEST_TIMEOUT;
if ($request->containsFileUploads()) {
$timeOut = static::DEFAULT_FILE_UPLOAD_REQUEST_TIMEOUT;
} elseif ($request->containsVideoUploads()) {
$timeOut = static::DEFAULT_VIDEO_UPLOAD_REQUEST_TIMEOUT;
}

// Should throw `FacebookSDKException` exception on HTTP client error.
// Don't catch to allow it to bubble up.
$rawResponse = $this->httpClientHandler->send($url, $method, $body, $headers, $timeOut);
$psr7Response = $this->httpClient->sendRequest(
MessageFactoryDiscovery::find()->createRequest($method, $url, $headers, $body)
);

static::$requestCount++;

$returnResponse = new FacebookResponse(
// Prepare headers from associative array to a single string for each header.
$responseHeaders = [];
foreach ($psr7Response->getHeaders() as $name => $values) {
$responseHeaders[] = sprintf('%s: %s', $name, implode(", ", $values));
}

$facebookResponse = new FacebookResponse(
$request,
$rawResponse->getBody(),
$rawResponse->getHttpResponseCode(),
$rawResponse->getHeaders()
$psr7Response->getBody(),
$psr7Response->getStatusCode(),
$responseHeaders
);

if ($returnResponse->isError()) {
throw $returnResponse->getThrownException();
if ($facebookResponse->isError()) {
throw $facebookResponse->getThrownException();
}

return $returnResponse;
return $facebookResponse;
}

/**
Expand Down
137 changes: 0 additions & 137 deletions src/Http/GraphRawResponse.php

This file was deleted.

Loading