forked from lifeibest/php-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.php
433 lines (359 loc) · 11.9 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* https://github.com/lifeibest/php-curl
define('_COOKIE_FILE', '/tmp/cookie_');
//for get
$curl = new Curl();
$respone = $curl->get('http://test.com', array('key'=>'a'));
//for post
$curl = new Curl();
$respone = $curl->post('http://test.com', array('key'=>'a'));
// post a file
$curl = new Curl();
$respone = $curl->post('http://test.com', array('key'=>'a','file'=>'@'.'exist_file'), 1);
// how get the cookie from header
extractCookies(file_get_contents(_COOKIE_FILE));
* @author [email protected]
*
**/
class Curl {
protected $_status;
protected $_info;
/**
* The file to read and write cookies to for requests
*
* @var string _COOKIE_FILE
**/
public $cookie_file;
/**
* Determines whether or not requests should follow redirects
*
* @var boolean
**/
public $follow_redirects = true;
/**
* An associative array of headers to send along with requests
*
* @var array
**/
public $headers = array();
/**
* An associative array of CURLOPT options to send along with requests
*
* @var array
**/
public $options = array();
/**
* The referer header to send along with requests
*
* @var string
**/
public $referer;
/**
* The user agent to send along with requests
*
* @var string
**/
public $user_agent;
/**
* Stores an error string for the last request if one occurred
*
* @var string
* @access protected
**/
protected $error = '';
/**
* Stores resource handle for the current CURL request
*
* @var resource
* @access protected
**/
public $request;
/**
* Initializes a Curl object
*
* Sets the $cookie_file to "curl_cookie.txt" in the current directory
* Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
**/
function __construct() {
$this->cookie_file = _COOKIE_FILE; //定义常量
$this->user_agent = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0 Adobe Flash Player 11\r\n';
}
/**
* Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse object
**/
function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}
/**
* Returns the error string of the current request if one occurred
*
* @return string
**/
function getError() {
return $this->error;
}
/**
* Makes an HTTP GET request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function get($url, $vars = array()) {
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}
return $this->request('GET', $url);
}
/**
* Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function head($url, $vars = array()) {
return $this->request('HEAD', $url, $vars);
}
/**
* Makes an HTTP POST request to the specified $url with an optional array or string of $vars
*
* @param string $url
* @param array|string $vars
* @param $type 0 normal 1 post files //fly
* @return CurlResponse|boolean
**/
function post($url, $vars = array(), $type = 0) {
return $this->request('POST', $url, $vars, $type);
}
/**
* Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function put($url, $vars = array()) {
return $this->request('PUT', $url, $vars);
}
/**
* Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $method
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function request($method, $url, $vars = array(), $type = 0) {
$this->error = '';
$this->request = curl_init();
//@ $type=1
if (!$type && is_array($vars)) $vars = http_build_query($vars, '', '&');
$this->set_request_method($method);
$this->set_request_options($url, $vars);
$this->set_request_headers();
$response = curl_exec($this->request);
//改变了顺序
if ($response === false) {
$this->error = curl_errno($this->request).' - '.curl_error($this->request);
} else {
$response = new CurlResponse($response);
}
//$headers = curl_getinfo($this->request); print_r($headers);exit;
$this->_info = curl_getinfo($this->request);
//$this->_status = curl_getinfo($this->request,CURLINFO_HTTP_CODE);
$this->_status = $this->_info['http_code'];
curl_close($this->request);
return $response;
}
public function getHttpStatus()
{
return $this->_status;
}
public function getInfo()
{
return $this->_info;
}
/**
* Formats and adds custom headers to the current request
*
* @return void
* @access protected
**/
protected function set_request_headers() {
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key.': '.$value;
}
curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
}
/**
* Set the associated CURL options for a request method
*
* @param string $method
* @return void
* @access protected
**/
protected function set_request_method($method) {
switch (strtoupper($method)) {
case 'HEAD':
curl_setopt($this->request, CURLOPT_NOBODY, true);
break;
case 'GET':
curl_setopt($this->request, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($this->request, CURLOPT_POST, true);
break;
default:
curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
}
}
/**
* Sets the CURLOPT options for the current request
*
* @param string $url
* @param string $vars
* @return void
* @access protected
**/
protected function set_request_options($url, $vars) {
curl_setopt($this->request, CURLOPT_URL, $url);
if (!empty($vars)) curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
# Set some default CURL options
curl_setopt($this->request, CURLOPT_HEADER, true);
curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
//curl_setopt($this->request, CURLOPT_TIMEOUT, 100); //执行时间秒
if ($this->cookie_file) {
curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
}
if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
# Set any custom CURL options
foreach ($this->options as $option => $value) {
curl_setopt($this->request, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Parses the response from a Curl request into an object containing
* the response body and an associative array of headers
*
* @package curl
* @author Sean Huber <[email protected]>
**/
class CurlResponse {
/**
* The body of the response without the headers block
*
* @var string
**/
public $body = '';
/**
* An associative array containing the response's headers
*
* @var array
**/
public $headers = array();
/**
* Accepts the result of a curl request as a string
*
* <code>
* $response = new CurlResponse(curl_exec($curl_handle));
* echo $response->body;
* echo $response->headers['Status'];
* </code>
*
* @param string $response
**/
function __construct($response) {
# Headers regex
$pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
# Extract headers from response
preg_match_all($pattern, $response, $matches);
$headers_string = array_pop($matches[0]);
$headers = explode("\r\n", str_replace("\r\n\r\n", '', $headers_string));
# Remove headers from the response body
$this->body = str_replace($headers_string, '', $response);
# Extract the version and status from the first header
$version_and_status = array_shift($headers);
preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
$this->headers['Http-Version'] = $matches[1];
$this->headers['Status-Code'] = $matches[2];
$this->headers['Status'] = $matches[2].' '.$matches[3];
# Convert headers into an associative array
foreach ($headers as $header) {
if ($matches[1] == 'Set-Cookie') {
$this->headers['cookie'][] = $matches[2];
} else {
$this->headers[$matches[1]] = $matches[2];
}
}
}
/**
* Returns the response body
*
* <code>
* $curl = new Curl;
* $response = $curl->get('google.com');
* echo $response; # => echo $response->body;
* </code>
*
* @return string
**/
function __toString() {
return $this->body;
}
}
//获取cookie
//extractCookies(file_get_contents(_COOKIE_FILE));
function extractCookies($string) {
$cookies = array();
$lines = explode("\n", $string);
// iterate over lines
foreach ($lines as $line) {
// we only care for valid cookie def lines
if (isset($line[0]) && substr_count($line, "\t") == 6) {
// get tokens in an array
$tokens = explode("\t", $line);
// trim the tokens
$tokens = array_map('trim', $tokens);
$cookie = array();
/*
// Extract the data
$cookie['domain'] = $tokens[0];
$cookie['flag'] = $tokens[1];
$cookie['path'] = $tokens[2];
$cookie['secure'] = $tokens[3];
// Convert date to a readable format
$cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
$cookie['name'] = $tokens[5];
$cookie['value'] = $tokens[6];
// Record the cookie.
$cookies[] = $cookie;
*/
$k = $tokens[5];
$cookies[$k] = $tokens[6];
}
}
return $cookies;
}