FacebookGuzzleHttpClient.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright 2017 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook\HttpClients;
  25. use Facebook\Http\GraphRawResponse;
  26. use Facebook\Exceptions\FacebookSDKException;
  27. use GuzzleHttp\Client;
  28. use GuzzleHttp\Message\ResponseInterface;
  29. use GuzzleHttp\Ring\Exception\RingException;
  30. use GuzzleHttp\Exception\RequestException;
  31. class FacebookGuzzleHttpClient implements FacebookHttpClientInterface
  32. {
  33. /**
  34. * @var \GuzzleHttp\Client The Guzzle client.
  35. */
  36. protected $guzzleClient;
  37. /**
  38. * @param \GuzzleHttp\Client|null The Guzzle client.
  39. */
  40. public function __construct(Client $guzzleClient = null)
  41. {
  42. $this->guzzleClient = $guzzleClient ?: new Client();
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function send($url, $method, $body, array $headers, $timeOut)
  48. {
  49. $options = [
  50. 'headers' => $headers,
  51. 'body' => $body,
  52. 'timeout' => $timeOut,
  53. 'connect_timeout' => 10,
  54. 'verify' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
  55. ];
  56. $request = $this->guzzleClient->createRequest($method, $url, $options);
  57. try {
  58. $rawResponse = $this->guzzleClient->send($request);
  59. } catch (RequestException $e) {
  60. $rawResponse = $e->getResponse();
  61. if ($e->getPrevious() instanceof RingException || !$rawResponse instanceof ResponseInterface) {
  62. throw new FacebookSDKException($e->getMessage(), $e->getCode());
  63. }
  64. }
  65. $rawHeaders = $this->getHeadersAsString($rawResponse);
  66. $rawBody = $rawResponse->getBody();
  67. $httpStatusCode = $rawResponse->getStatusCode();
  68. return new GraphRawResponse($rawHeaders, $rawBody, $httpStatusCode);
  69. }
  70. /**
  71. * Returns the Guzzle array of headers as a string.
  72. *
  73. * @param ResponseInterface $response The Guzzle response.
  74. *
  75. * @return string
  76. */
  77. public function getHeadersAsString(ResponseInterface $response)
  78. {
  79. $headers = $response->getHeaders();
  80. $rawHeaders = [];
  81. foreach ($headers as $name => $values) {
  82. $rawHeaders[] = $name . ": " . implode(", ", $values);
  83. }
  84. return implode("\r\n", $rawHeaders);
  85. }
  86. }