PluginProbe
Loginizer / trunk
Loginizer vtrunk
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / lib / hybridauth / HttpClient / Curl.php

Curl.php in Loginizer trunk, at lib/hybridauth/HttpClient/Curl.php

317 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*!
3 * Hybridauth
4 * https://hybridauth.github.io | https://github.com/hybridauth/hybridauth
5 * (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html
6 */
7
8 namespace Hybridauth\HttpClient;
9
10 /**
11 * Hybridauth default Http client
12 */
13 class Curl implements HttpClientInterface
14 {
15 /**
16 * Default curl options
17 *
18 * These defaults options can be overwritten when sending requests.
19 *
20 * See setCurlOptions()
21 *
22 * @var array
23 */
24 protected $curlOptions = [
25 CURLOPT_TIMEOUT => 30,
26 CURLOPT_CONNECTTIMEOUT => 30,
27 CURLOPT_SSL_VERIFYPEER => false,
28 CURLOPT_SSL_VERIFYHOST => false,
29 CURLOPT_RETURNTRANSFER => true,
30 CURLOPT_FOLLOWLOCATION => true,
31 CURLOPT_MAXREDIRS => 5,
32 CURLINFO_HEADER_OUT => true,
33 CURLOPT_ENCODING => 'identity',
34 // phpcs:ignore
35 CURLOPT_USERAGENT => 'Hybridauth, PHP Social Authentication Library (https://github.com/hybridauth/hybridauth)',
36 ];
37
38 /**
39 * Method request() arguments
40 *
41 * This is used for debugging.
42 *
43 * @var array
44 */
45 protected $requestArguments = [];
46
47 /**
48 * Default request headers
49 *
50 * @var array
51 */
52 protected $requestHeader = [
53 'Accept' => '*/*',
54 'Cache-Control' => 'max-age=0',
55 'Connection' => 'keep-alive',
56 'Expect' => '',
57 'Pragma' => '',
58 ];
59
60 /**
61 * Raw response returned by server
62 *
63 * @var string
64 */
65 protected $responseBody = '';
66
67 /**
68 * Headers returned in the response
69 *
70 * @var array
71 */
72 protected $responseHeader = [];
73
74 /**
75 * Response HTTP status code
76 *
77 * @var int
78 */
79 protected $responseHttpCode = 0;
80
81 /**
82 * Last curl error number
83 *
84 * @var mixed
85 */
86 protected $responseClientError = null;
87
88 /**
89 * Information about the last transfer
90 *
91 * @var mixed
92 */
93 protected $responseClientInfo = [];
94
95 /**
96 * Hybridauth logger instance
97 *
98 * @var object
99 */
100 protected $logger = null;
101
102 /**
103 * {@inheritdoc}
104 */
105 public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false)
106 {
107 $this->requestHeader = array_replace($this->requestHeader, (array)$headers);
108
109 $this->requestArguments = [
110 'uri' => $uri,
111 'method' => $method,
112 'parameters' => $parameters,
113 'headers' => $this->requestHeader,
114 ];
115
116 $curl = curl_init();
117
118 switch ($method) {
119 case 'GET':
120 case 'DELETE':
121 unset($this->curlOptions[CURLOPT_POST]);
122 unset($this->curlOptions[CURLOPT_POSTFIELDS]);
123
124 $uri = $uri . (strpos($uri, '?') ? '&' : '?') . http_build_query($parameters);
125 if ($method === 'DELETE') {
126 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = 'DELETE';
127 }
128 break;
129 case 'PUT':
130 case 'POST':
131 case 'PATCH':
132 $body_content = $multipart ? $parameters : http_build_query($parameters);
133 if (isset($this->requestHeader['Content-Type'])
134 && $this->requestHeader['Content-Type'] == 'application/json'
135 ) {
136 $body_content = json_encode($parameters);
137 }
138
139 if ($method === 'POST') {
140 $this->curlOptions[CURLOPT_POST] = true;
141 } else {
142 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
143 }
144 $this->curlOptions[CURLOPT_POSTFIELDS] = $body_content;
145 break;
146 }
147
148 $this->curlOptions[CURLOPT_URL] = $uri;
149 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->prepareRequestHeaders();
150 $this->curlOptions[CURLOPT_HEADERFUNCTION] = [$this, 'fetchResponseHeader'];
151
152 foreach ($this->curlOptions as $opt => $value) {
153 curl_setopt($curl, $opt, $value);
154 }
155
156 $response = curl_exec($curl);
157
158 $this->responseBody = $response;
159 $this->responseHttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
160 $this->responseClientError = curl_error($curl);
161 $this->responseClientInfo = curl_getinfo($curl);
162
163 if ($this->logger) {
164 // phpcs:ignore
165 $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse());
166
167 if (false === $response) {
168 // phpcs:ignore
169 $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]);
170 }
171 }
172
173 curl_close($curl);
174
175 return $this->responseBody;
176 }
177
178 /**
179 * Get response details
180 *
181 * @return array Map structure of details
182 */
183 public function getResponse()
184 {
185 $curlOptions = $this->curlOptions;
186
187 $curlOptions[CURLOPT_HEADERFUNCTION] = '*omitted';
188
189 return [
190 'request' => $this->getRequestArguments(),
191 'response' => [
192 'code' => $this->getResponseHttpCode(),
193 'headers' => $this->getResponseHeader(),
194 'body' => $this->getResponseBody(),
195 ],
196 'client' => [
197 'error' => $this->getResponseClientError(),
198 'info' => $this->getResponseClientInfo(),
199 'opts' => $curlOptions,
200 ],
201 ];
202 }
203
204 /**
205 * Reset curl options
206 *
207 * @param array $curlOptions
208 */
209 public function setCurlOptions($curlOptions)
210 {
211 foreach ($curlOptions as $opt => $value) {
212 $this->curlOptions[$opt] = $value;
213 }
214 }
215
216 /**
217 * Set logger instance
218 *
219 * @param object $logger
220 */
221 public function setLogger($logger)
222 {
223 $this->logger = $logger;
224 }
225
226 /**
227 * {@inheritdoc}
228 */
229 public function getResponseBody()
230 {
231 return $this->responseBody;
232 }
233
234 /**
235 * {@inheritdoc}
236 */
237 public function getResponseHeader()
238 {
239 return $this->responseHeader;
240 }
241
242 /**
243 * {@inheritdoc}
244 */
245 public function getResponseHttpCode()
246 {
247 return $this->responseHttpCode;
248 }
249
250 /**
251 * {@inheritdoc}
252 */
253 public function getResponseClientError()
254 {
255 return $this->responseClientError;
256 }
257
258 /**
259 * @return array
260 */
261 protected function getResponseClientInfo()
262 {
263 return $this->responseClientInfo;
264 }
265
266 /**
267 * Returns method request() arguments
268 *
269 * This is used for debugging.
270 *
271 * @return array
272 */
273 protected function getRequestArguments()
274 {
275 return $this->requestArguments;
276 }
277
278 /**
279 * Fetch server response headers
280 *
281 * @param mixed $curl
282 * @param string $header
283 *
284 * @return int
285 */
286 protected function fetchResponseHeader($curl, $header)
287 {
288 $pos = strpos($header, ':');
289
290 if (!empty($pos)) {
291 $key = str_replace('-', '_', strtolower(substr($header, 0, $pos)));
292
293 $value = trim(substr($header, $pos + 2));
294
295 $this->responseHeader[$key] = $value;
296 }
297
298 return strlen($header);
299 }
300
301 /**
302 * Convert request headers to the expect curl format
303 *
304 * @return array
305 */
306 protected function prepareRequestHeaders()
307 {
308 $headers = [];
309
310 foreach ($this->requestHeader as $header => $value) {
311 $headers[] = trim($header) . ': ' . trim($value);
312 }
313
314 return $headers;
315 }
316 }
317