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