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

Update FacebookGuzzleHttpClient.php file with Guzzle 6.x which is als… #1165

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
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
56 changes: 18 additions & 38 deletions src/Facebook/HttpClients/FacebookGuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Facebook\Exceptions\FacebookSDKException;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Ring\Exception\RingException;
use GuzzleHttp\Exception\RequestException;
Expand All @@ -41,57 +42,36 @@ class FacebookGuzzleHttpClient implements FacebookHttpClientInterface
/**
* @param \GuzzleHttp\Client|null The Guzzle client.
*/
public function __construct(Client $guzzleClient = null)
public function __construct()
{
$this->guzzleClient = $guzzleClient ?: new Client();
$this->guzzleClient = new Client();
}

/**
* @inheritdoc
*/
public function send($url, $method, $body, array $headers, $timeOut)
{
$options = [
'headers' => $headers,
'body' => $body,
'timeout' => $timeOut,
'connect_timeout' => 10,
'verify' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
];
$request = $this->guzzleClient->createRequest($method, $url, $options);
$request = new Request($method, $url, $headers, $body);

try {
$rawResponse = $this->guzzleClient->send($request);
} catch (RequestException $e) {
$rawResponse = $e->getResponse();

if ($e->getPrevious() instanceof RingException || !$rawResponse instanceof ResponseInterface) {
throw new FacebookSDKException($e->getMessage(), $e->getCode());
}
$response = $this->guzzleClient->send($request, ['timeout' => $timeOut, 'http_errors' => false]);
} catch (GuzzleHttp\Exception\RequestException $e) {
throw new FacebookSDKException($e->getMessage(), $e->getCode());
}

$rawHeaders = $this->getHeadersAsString($rawResponse);
$rawBody = $rawResponse->getBody();
$httpStatusCode = $rawResponse->getStatusCode();

return new GraphRawResponse($rawHeaders, $rawBody, $httpStatusCode);
}

/**
* Returns the Guzzle array of headers as a string.
*
* @param ResponseInterface $response The Guzzle response.
*
* @return string
*/
public function getHeadersAsString(ResponseInterface $response)
{
$headers = $response->getHeaders();
$rawHeaders = [];
foreach ($headers as $name => $values) {
$rawHeaders[] = $name . ": " . implode(", ", $values);
$httpStatusCode = $response->getStatusCode();
$responseHeaders = $response->getHeaders();
foreach ($responseHeaders as $key => $values) {
$responseHeaders[$key] = implode(', ', $values);
}

return implode("\r\n", $rawHeaders);
$responseBody = $response->getBody()->getContents();

return new GraphRawResponse(
$responseHeaders,
$responseBody,
$httpStatusCode
);
}
}