1 | <?php |
||
34 | class FacebookCurlHttpClient implements FacebookHttpClientInterface |
||
35 | { |
||
36 | /** |
||
37 | * @var string The client error message |
||
38 | */ |
||
39 | protected $curlErrorMessage = ''; |
||
40 | |||
41 | /** |
||
42 | * @var int The curl client error code |
||
43 | */ |
||
44 | protected $curlErrorCode = 0; |
||
45 | |||
46 | /** |
||
47 | * @var string|boolean The raw response from the server |
||
48 | */ |
||
49 | protected $rawResponse; |
||
50 | |||
51 | /** |
||
52 | * @var FacebookCurl Procedural curl as object |
||
53 | */ |
||
54 | protected $facebookCurl; |
||
55 | |||
56 | /** |
||
57 | * @param FacebookCurl|null Procedural curl as object |
||
58 | */ |
||
59 | public function __construct(FacebookCurl $facebookCurl = null) |
||
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | public function send($url, $method, $body, array $headers, $timeOut) |
||
68 | { |
||
69 | $this->openConnection($url, $method, $body, $headers, $timeOut); |
||
70 | $this->sendRequest(); |
||
71 | |||
72 | if ($curlErrorCode = $this->facebookCurl->errno()) { |
||
73 | throw new FacebookSDKException($this->facebookCurl->error(), $curlErrorCode); |
||
74 | } |
||
75 | |||
76 | // Separate the raw headers from the raw body |
||
77 | list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody(); |
||
78 | |||
79 | $this->closeConnection(); |
||
80 | |||
81 | return new GraphRawResponse($rawHeaders, $rawBody); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Opens a new curl connection. |
||
86 | * |
||
87 | * @param string $url The endpoint to send the request to. |
||
88 | * @param string $method The request method. |
||
89 | * @param string $body The body of the request. |
||
90 | * @param array $headers The request headers. |
||
91 | * @param int $timeOut The timeout in seconds for the request. |
||
92 | */ |
||
93 | public function openConnection($url, $method, $body, array $headers, $timeOut) |
||
94 | { |
||
95 | $options = [ |
||
96 | CURLOPT_CUSTOMREQUEST => $method, |
||
97 | CURLOPT_HTTPHEADER => $this->compileRequestHeaders($headers), |
||
98 | CURLOPT_URL => $url, |
||
99 | CURLOPT_CONNECTTIMEOUT => 10, |
||
100 | CURLOPT_TIMEOUT => $timeOut, |
||
101 | CURLOPT_RETURNTRANSFER => true, // Follow 301 redirects |
||
102 | CURLOPT_HEADER => true, // Enable header processing |
||
103 | CURLOPT_SSL_VERIFYHOST => 2, |
||
104 | CURLOPT_SSL_VERIFYPEER => true, |
||
105 | CURLOPT_CAINFO => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', |
||
106 | ]; |
||
107 | |||
108 | if ($method !== "GET") { |
||
109 | $options[CURLOPT_POSTFIELDS] = $body; |
||
110 | } |
||
111 | |||
112 | $this->facebookCurl->init(); |
||
113 | $this->facebookCurl->setoptArray($options); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Closes an existing curl connection |
||
118 | */ |
||
119 | public function closeConnection() |
||
120 | { |
||
121 | $this->facebookCurl->close(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Send the request and get the raw response from curl |
||
126 | */ |
||
127 | public function sendRequest() |
||
128 | { |
||
129 | $this->rawResponse = $this->facebookCurl->exec(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Compiles the request headers into a curl-friendly format. |
||
134 | * |
||
135 | * @param array $headers The request headers. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function compileRequestHeaders(array $headers) |
||
140 | { |
||
141 | $return = []; |
||
142 | |||
143 | foreach ($headers as $key => $value) { |
||
144 | $return[] = $key . ': ' . $value; |
||
145 | } |
||
146 | |||
147 | return $return; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Extracts the headers and the body into a two-part array |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function extractResponseHeadersAndBody() |
||
163 | } |
||
164 |