-
Notifications
You must be signed in to change notification settings - Fork 498
/
cURL.php
executable file
·349 lines (299 loc) · 9.81 KB
/
cURL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* cURL HTTP transport
*
* @package Requests
* @subpackage Transport
*/
/**
* cURL HTTP transport
*
* @package Requests
* @subpackage Transport
*/
class Requests_Transport_cURL implements Requests_Transport {
/**
* Raw HTTP data
*
* @var string
*/
public $headers = '';
/**
* Information on the current request
*
* @var array cURL information array, see {@see http://php.net/curl_getinfo}
*/
public $info;
/**
* Version string
*
* @var string
*/
public $version;
/**
* cURL handle
*
* @var resource
*/
protected $fp;
/**
* Have we finished the headers yet?
*
* @var boolean
*/
protected $done_headers = false;
/**
* If streaming to a file, keep the file pointer
*
* @var resource
*/
protected $stream_handle;
/**
* Constructor
*/
public function __construct() {
$curl = curl_version();
$this->version = $curl['version'];
$this->fp = curl_init();
curl_setopt($this->fp, CURLOPT_HEADER, false);
curl_setopt($this->fp, CURLOPT_RETURNTRANSFER, 1);
if (version_compare($this->version, '7.10.5', '>=')) {
curl_setopt($this->fp, CURLOPT_ENCODING, '');
}
if (version_compare($this->version, '7.19.4', '>=')) {
curl_setopt($this->fp, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
}
/**
* Perform a request
*
* @throws Requests_Exception On a cURL error (`curlerror`)
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
* @return string Raw HTTP result
*/
public function request($url, $headers = array(), $data = array(), $options = array()) {
$this->setup_handle($url, $headers, $data, $options);
$options['hooks']->dispatch('curl.before_send', array(&$this->fp));
if ($options['filename'] !== false) {
$this->stream_handle = fopen($options['filename'], 'wb');
curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
}
if (isset($options['verify'])) {
if ($options['verify'] === false) {
curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->fp, CURLOPT_SSL_VERIFYPEER, 0);
} elseif (is_string($options['verify'])) {
curl_setopt($this->fp, CURLOPT_CAINFO, $options['verify']);
}
}
if (isset($options['verifyname']) && $options['verifyname'] === false) {
curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
}
$response = curl_exec($this->fp);
$options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
if (curl_errno($this->fp) === 23 || curl_errno($this->fp) === 61) {
curl_setopt($this->fp, CURLOPT_ENCODING, 'none');
$response = curl_exec($this->fp);
}
$this->process_response($response, $options);
return $this->headers;
}
/**
* Send multiple requests simultaneously
*
* @param array $requests Request data
* @param array $options Global options
* @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
*/
public function request_multiple($requests, $options) {
$multihandle = curl_multi_init();
$subrequests = array();
$subhandles = array();
$class = get_class($this);
foreach ($requests as $id => $request) {
$subrequests[$id] = new $class();
$subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
$request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
curl_multi_add_handle($multihandle, $subhandles[$id]);
}
$completed = 0;
$responses = array();
$request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
do {
$active = false;
do {
$status = curl_multi_exec($multihandle, $active);
}
while ($status === CURLM_CALL_MULTI_PERFORM);
$to_process = array();
// Read the information as needed
while ($done = curl_multi_info_read($multihandle)) {
$key = array_search($done['handle'], $subhandles, true);
if (!isset($to_process[$key])) {
$to_process[$key] = $done;
}
}
// Parse the finished requests before we start getting the new ones
foreach ($to_process as $key => $done) {
$options = $requests[$key]['options'];
$responses[$key] = $subrequests[$key]->process_response(curl_multi_getcontent($done['handle']), $options);
$options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
curl_multi_remove_handle($multihandle, $done['handle']);
curl_close($done['handle']);
if (!is_string($responses[$key])) {
$options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
}
$completed++;
}
}
while ($active || $completed < count($subrequests));
$request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
curl_multi_close($multihandle);
return $responses;
}
/**
* Get the cURL handle for use in a multi-request
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
* @return resource Subrequest's cURL handle
*/
public function &get_subrequest_handle($url, $headers, $data, $options) {
$this->setup_handle($url, $headers, $data, $options);
if ($options['filename'] !== false) {
$this->stream_handle = fopen($options['filename'], 'wb');
curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
}
return $this->fp;
}
/**
* Setup the cURL handle for the given data
*
* @param string $url URL to request
* @param array $headers Associative array of request headers
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @param array $options Request options, see {@see Requests::response()} for documentation
*/
protected function setup_handle($url, $headers, $data, $options) {
$options['hooks']->dispatch('curl.before_request', array(&$this->fp));
$headers = Requests::flatten($headers);
if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
$url = self::format_get($url, $data);
}
elseif (!empty($data) && !is_string($data)) {
$data = http_build_query($data, null, '&');
}
switch ($options['type']) {
case Requests::POST:
curl_setopt($this->fp, CURLOPT_POST, true);
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::PATCH:
case Requests::PUT:
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::DELETE:
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case Requests::HEAD:
curl_setopt($this->fp, CURLOPT_NOBODY, true);
break;
}
curl_setopt($this->fp, CURLOPT_URL, $url);
curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_REFERER, $url);
curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
if (true === $options['blocking']) {
curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
}
}
public function process_response($response, $options) {
if ($options['blocking'] === false) {
curl_close($this->fp);
$fake_headers = '';
$options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
return false;
}
if ($options['filename'] !== false) {
fclose($this->stream_handle);
$this->headers = trim($this->headers);
}
else {
$this->headers .= $response;
}
if (curl_errno($this->fp)) {
throw new Requests_Exception('cURL error ' . curl_errno($this->fp) . ': ' . curl_error($this->fp), 'curlerror', $this->fp);
return;
}
$this->info = curl_getinfo($this->fp);
curl_close($this->fp);
$options['hooks']->dispatch('curl.after_request', array(&$this->headers));
return $this->headers;
}
/**
* Collect the headers as they are received
*
* @param resource $handle cURL resource
* @param string $headers Header string
* @return integer Length of provided header
*/
protected function stream_headers($handle, $headers) {
// Why do we do this? cURL will send both the final response and any
// interim responses, such as a 100 Continue. We don't need that.
// (We may want to keep this somewhere just in case)
if ($this->done_headers) {
$this->headers = '';
$this->done_headers = false;
}
$this->headers .= $headers;
if ($headers === "\r\n") {
$this->done_headers = true;
}
return strlen($headers);
}
/**
* Format a URL given GET data
*
* @param string $url
* @param array|object $data Data to build query using, see {@see http://php.net/http_build_query}
* @return string URL with data
*/
protected static function format_get($url, $data) {
if (!empty($data)) {
$url_parts = parse_url($url);
if (empty($url_parts['query'])) {
$query = $url_parts['query'] = '';
}
else {
$query = $url_parts['query'];
}
$query .= '&' . http_build_query($data, null, '&');
$query = trim($query, '&');
if (empty($url_parts['query'])) {
$url .= '?' . $query;
}
else {
$url = str_replace($url_parts['query'], $query, $url);
}
}
return $url;
}
/**
* Whether this transport is valid
*
* @codeCoverageIgnore
* @return boolean True if the transport is valid, false otherwise.
*/
public static function test() {
return (function_exists('curl_init') && function_exists('curl_exec'));
}
}