FacebookStreamHttpClient.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class FacebookStreamHttpClient implements FacebookHttpClientInterface
  28. {
  29. /**
  30. * @var FacebookStream Procedural stream wrapper as object.
  31. */
  32. protected $facebookStream;
  33. /**
  34. * @param FacebookStream|null Procedural stream wrapper as object.
  35. */
  36. public function __construct(FacebookStream $facebookStream = null)
  37. {
  38. $this->facebookStream = $facebookStream ?: new FacebookStream();
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function send($url, $method, $body, array $headers, $timeOut)
  44. {
  45. $options = [
  46. 'http' => [
  47. 'method' => $method,
  48. 'header' => $this->compileHeader($headers),
  49. 'content' => $body,
  50. 'timeout' => $timeOut,
  51. 'ignore_errors' => true
  52. ],
  53. 'ssl' => [
  54. 'verify_peer' => true,
  55. 'verify_peer_name' => true,
  56. 'allow_self_signed' => true, // All root certificates are self-signed
  57. 'cafile' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
  58. ],
  59. ];
  60. $this->facebookStream->streamContextCreate($options);
  61. $rawBody = $this->facebookStream->fileGetContents($url);
  62. $rawHeaders = $this->facebookStream->getResponseHeaders();
  63. if ($rawBody === false || empty($rawHeaders)) {
  64. throw new FacebookSDKException('Stream returned an empty response', 660);
  65. }
  66. $rawHeaders = implode("\r\n", $rawHeaders);
  67. return new GraphRawResponse($rawHeaders, $rawBody);
  68. }
  69. /**
  70. * Formats the headers for use in the stream wrapper.
  71. *
  72. * @param array $headers The request headers.
  73. *
  74. * @return string
  75. */
  76. public function compileHeader(array $headers)
  77. {
  78. $header = [];
  79. foreach ($headers as $k => $v) {
  80. $header[] = $k . ': ' . $v;
  81. }
  82. return implode("\r\n", $header);
  83. }
  84. }