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

Commit 6bb017a

Browse files
authored
Merge pull request #641 from Nyholm/httplug
Removed the HTTP layer and started to use HTTPlug
2 parents 1f889b4 + c9433ad commit 6bb017a

27 files changed

+128
-1712
lines changed

CHANGELOG.md

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

1519
## 5.x
1620

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ This repository contains the open source PHP SDK that allows you to access the F
1515
The Facebook PHP SDK can be installed with [Composer](https://getcomposer.org/). Run this command:
1616

1717
```sh
18-
composer require facebook/graph-sdk
18+
composer require facebook/graph-sdk php-http/curl-client guzzlehttp/psr7
1919
```
2020

21+
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).
22+
2123

2224
## Usage
2325

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
"sort-packages": true
1616
},
1717
"require": {
18-
"php": "^7.1"
18+
"php": "^7.1",
19+
"psr/http-message": "^1.0",
20+
"php-http/client-implementation": "^1.0",
21+
"php-http/httplug": "^1.0",
22+
"php-http/discovery": "^1.0",
23+
24+
"php-http/message": "^1.0"
1925
},
2026
"require-dev": {
21-
"guzzlehttp/guzzle": "^5.3.1",
22-
"phpunit/phpunit": "^6.2"
23-
},
24-
"suggest": {
25-
"guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
27+
"phpunit/phpunit": "^6.2",
28+
"php-http/guzzle6-adapter": "^1.0"
2629
},
2730
"autoload": {
2831
"psr-4": {

docs/reference/FacebookVideo.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $myVideoFileToUpload = $fb->videoToUpload('/path/to/video-file.mp4'),
2424

2525
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).
2626

27+
> **Warning:** Uploading videos may cause a timeout. Make sure to configure your HTTP client to increase timeout time before uploading videos.
28+
2729
## Usage
2830

2931
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).

src/Facebook.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
use Facebook\GraphNodes\GraphEdge;
3333
use Facebook\Url\UrlDetectionInterface;
3434
use Facebook\Url\FacebookUrlDetectionHandler;
35-
use Facebook\HttpClients\HttpClientsFactory;
3635
use Facebook\PersistentData\PersistentDataFactory;
3736
use Facebook\PersistentData\PersistentDataInterface;
3837
use Facebook\Helpers\FacebookCanvasHelper;
3938
use Facebook\Helpers\FacebookJavaScriptHelper;
4039
use Facebook\Helpers\FacebookPageTabHelper;
4140
use Facebook\Helpers\FacebookRedirectLoginHelper;
4241
use Facebook\Exceptions\FacebookSDKException;
42+
use Http\Client\HttpClient;
4343

4444
/**
4545
* Class Facebook
@@ -117,7 +117,7 @@ public function __construct(array $config = [])
117117
'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
118118
'default_graph_version' => null,
119119
'enable_beta_mode' => false,
120-
'http_client_handler' => null,
120+
'http_client' => null,
121121
'persistent_data_handler' => null,
122122
'url_detection_handler' => null,
123123
], $config);
@@ -128,13 +128,16 @@ public function __construct(array $config = [])
128128
if (!$config['app_secret']) {
129129
throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
130130
}
131+
if ($config['http_client'] !== null && !$config['http_client'] instanceof HttpClient) {
132+
throw new \InvalidArgumentException('Required "http_client" key to be null or an instance of \Http\Client\HttpClient');
133+
}
131134
if (!$config['default_graph_version']) {
132135
throw new \InvalidArgumentException('Required "default_graph_version" key not supplied in config');
133136
}
134137

135138
$this->app = new FacebookApp($config['app_id'], $config['app_secret']);
136139
$this->client = new FacebookClient(
137-
HttpClientsFactory::createHttpClient($config['http_client_handler']),
140+
$config['http_client'],
138141
$config['enable_beta_mode']
139142
);
140143
$this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler());

src/FacebookClient.php

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
*/
2424
namespace Facebook;
2525

26-
use Facebook\HttpClients\FacebookHttpClientInterface;
27-
use Facebook\HttpClients\FacebookCurlHttpClient;
28-
use Facebook\HttpClients\FacebookStreamHttpClient;
2926
use Facebook\Exceptions\FacebookSDKException;
27+
use Http\Client\HttpClient;
28+
use Http\Discovery\HttpClientDiscovery;
29+
use Http\Discovery\MessageFactoryDiscovery;
3030

3131
/**
3232
* Class FacebookClient
@@ -76,9 +76,9 @@ class FacebookClient
7676
protected $enableBetaMode = false;
7777

7878
/**
79-
* @var FacebookHttpClientInterface HTTP client handler.
79+
* @var HttpClient HTTP client handler.
8080
*/
81-
protected $httpClientHandler;
81+
protected $httpClient;
8282

8383
/**
8484
* @var int The number of calls that have been made to Graph.
@@ -88,43 +88,33 @@ class FacebookClient
8888
/**
8989
* Instantiates a new FacebookClient object.
9090
*
91-
* @param FacebookHttpClientInterface|null $httpClientHandler
92-
* @param boolean $enableBeta
91+
* @param HttpClient|null $httpClient
92+
* @param boolean $enableBeta
9393
*/
94-
public function __construct(FacebookHttpClientInterface $httpClientHandler = null, $enableBeta = false)
94+
public function __construct(HttpClient $httpClient = null, $enableBeta = false)
9595
{
96-
$this->httpClientHandler = $httpClientHandler ?: $this->detectHttpClientHandler();
96+
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
9797
$this->enableBetaMode = $enableBeta;
9898
}
9999

100100
/**
101101
* Sets the HTTP client handler.
102102
*
103-
* @param FacebookHttpClientInterface $httpClientHandler
103+
* @param HttpClient $httpClient
104104
*/
105-
public function setHttpClientHandler(FacebookHttpClientInterface $httpClientHandler)
105+
public function setHttpClient(HttpClient $httpClient)
106106
{
107-
$this->httpClientHandler = $httpClientHandler;
107+
$this->httpClient = $httpClient;
108108
}
109109

110110
/**
111111
* Returns the HTTP client handler.
112112
*
113-
* @return FacebookHttpClientInterface
113+
* @return HttpClient
114114
*/
115-
public function getHttpClientHandler()
115+
public function getHttpClient()
116116
{
117-
return $this->httpClientHandler;
118-
}
119-
120-
/**
121-
* Detects which HTTP client handler to use.
122-
*
123-
* @return FacebookHttpClientInterface
124-
*/
125-
public function detectHttpClientHandler()
126-
{
127-
return extension_loaded('curl') ? new FacebookCurlHttpClient() : new FacebookStreamHttpClient();
117+
return $this->httpClient;
128118
}
129119

130120
/**
@@ -203,32 +193,30 @@ public function sendRequest(FacebookRequest $request)
203193

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

206-
// Since file uploads can take a while, we need to give more time for uploads
207-
$timeOut = static::DEFAULT_REQUEST_TIMEOUT;
208-
if ($request->containsFileUploads()) {
209-
$timeOut = static::DEFAULT_FILE_UPLOAD_REQUEST_TIMEOUT;
210-
} elseif ($request->containsVideoUploads()) {
211-
$timeOut = static::DEFAULT_VIDEO_UPLOAD_REQUEST_TIMEOUT;
212-
}
213-
214-
// Should throw `FacebookSDKException` exception on HTTP client error.
215-
// Don't catch to allow it to bubble up.
216-
$rawResponse = $this->httpClientHandler->send($url, $method, $body, $headers, $timeOut);
196+
$psr7Response = $this->httpClient->sendRequest(
197+
MessageFactoryDiscovery::find()->createRequest($method, $url, $headers, $body)
198+
);
217199

218200
static::$requestCount++;
219201

220-
$returnResponse = new FacebookResponse(
202+
// Prepare headers from associative array to a single string for each header.
203+
$responseHeaders = [];
204+
foreach ($psr7Response->getHeaders() as $name => $values) {
205+
$responseHeaders[] = sprintf('%s: %s', $name, implode(", ", $values));
206+
}
207+
208+
$facebookResponse = new FacebookResponse(
221209
$request,
222-
$rawResponse->getBody(),
223-
$rawResponse->getHttpResponseCode(),
224-
$rawResponse->getHeaders()
210+
$psr7Response->getBody(),
211+
$psr7Response->getStatusCode(),
212+
$responseHeaders
225213
);
226214

227-
if ($returnResponse->isError()) {
228-
throw $returnResponse->getThrownException();
215+
if ($facebookResponse->isError()) {
216+
throw $facebookResponse->getThrownException();
229217
}
230218

231-
return $returnResponse;
219+
return $facebookResponse;
232220
}
233221

234222
/**

src/Http/GraphRawResponse.php

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)