1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
3
|
|
|
require_once(FWOLFLIB . 'func/array.php'); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A class aimed to use curl function efficiency |
8
|
|
|
* |
9
|
|
|
* Very useful in write a game bot, or an information thief program. |
10
|
|
|
* |
11
|
|
|
* @deprecated Use Fwlib\Net\Curl |
12
|
|
|
* @package fwolflib |
13
|
|
|
* @copyright Copyright © 2007-2012, 2014, Fwolf |
14
|
|
|
* @author Fwolf <[email protected]> |
15
|
|
|
* @since 2007-03-14 |
16
|
|
|
*/ |
17
|
|
|
class Curl extends Fwolflib { |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* File to save cookie |
21
|
|
|
* @var string |
22
|
|
|
* @access protected |
23
|
|
|
*/ |
24
|
|
|
protected $mCookiefile = '/dev/null'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* File to save log |
28
|
|
|
* Set to empty string to direct echo out(default), |
29
|
|
|
* Or set to a file to save in, |
30
|
|
|
* Or set to /dev/null to do nothing. |
31
|
|
|
* @var string |
32
|
|
|
* @access public |
33
|
|
|
*/ |
34
|
|
|
public $mLogfile = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Result read from webserver |
38
|
|
|
* @var string |
39
|
|
|
* @access public |
40
|
|
|
*/ |
41
|
|
|
public $mRs = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Curl session resource |
45
|
|
|
* @var object |
46
|
|
|
* @access public |
47
|
|
|
*/ |
48
|
|
|
public $mSh; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Construct function |
53
|
|
|
* @access public |
54
|
|
|
*/ |
55
|
|
|
function __construct() { |
|
|
|
|
56
|
|
|
parent::__construct(); |
57
|
|
|
|
58
|
|
|
$this->mSh = curl_init(); |
|
|
|
|
59
|
|
|
$this->SetoptCommon(); |
60
|
|
|
} // end of func __construct |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Destruct function |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
|
|
function __destruct() { |
|
|
|
|
68
|
|
|
curl_close($this->mSh); |
69
|
|
|
|
70
|
|
|
// Write log to file |
71
|
|
|
if (!empty($this->mLogfile)) |
72
|
|
|
file_put_contents($this->mLogfile, $this->LogGet(), FILE_APPEND); |
73
|
|
|
|
74
|
|
|
parent::__destruct(); |
75
|
|
|
} // end of func __destruct |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Http get content from host |
80
|
|
|
* |
81
|
|
|
* @param string $url Host address |
82
|
|
|
* @param mixed $param Get parameter, can be string or array. |
83
|
|
|
* @access public |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function Get ($url, $param = '') { |
87
|
|
|
curl_setopt($this->mSh, CURLOPT_HTTPGET, true); |
88
|
|
|
|
89
|
|
|
// Remove endding '?" of url |
90
|
|
View Code Duplication |
if ('?' == substr($url, -1, 1)) |
|
|
|
|
91
|
|
|
$url = substr($url, 0, strlen($url - 1)); |
92
|
|
|
|
93
|
|
|
// Char used between url & param |
94
|
|
|
if (false === strpos($url, '?')) |
95
|
|
|
$s_linker = '?'; |
96
|
|
|
else |
97
|
|
|
$s_linker = '&'; |
98
|
|
|
|
99
|
|
|
// Parse param, join array and fix linker char with url |
100
|
|
View Code Duplication |
if (is_array($param) && 0 < count($param)) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$s = ''; |
103
|
|
|
foreach ($param as $k => $v) |
104
|
|
|
$s .= "&" . urlencode($k) . '=' . urlencode($v); |
105
|
|
|
$param = $s; |
106
|
|
|
} |
107
|
|
|
if (!empty($param)) |
108
|
|
|
$param{0} = $s_linker; |
109
|
|
|
|
110
|
|
|
//$this->Log($url . $param); |
111
|
|
|
curl_setopt($this->mSh, CURLOPT_URL, $url . $param); |
112
|
|
|
$this->mRs = curl_exec($this->mSh); |
113
|
|
|
|
114
|
|
|
if (0 != curl_errno($this->mSh)) |
115
|
|
|
$this->Log(curl_error($this->mSh)); |
116
|
|
|
|
117
|
|
|
return $this->mRs; |
118
|
|
|
} // end of func Get |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get server return code of last curl_exec |
123
|
|
|
* 200-ok, 404-missing file, etc... |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
public function GetLastCode() |
127
|
|
|
{ |
128
|
|
|
$i = curl_getinfo($this->mSh, CURLINFO_HTTP_CODE); |
129
|
|
|
return intval($i); |
130
|
|
|
} // end of func GetLastCode |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get server return content type of last curl_exec |
135
|
|
|
* text/html, image/png, etc... |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function GetLastContentType() |
139
|
|
|
{ |
140
|
|
|
$s = curl_getinfo($this->mSh, CURLINFO_CONTENT_TYPE); |
141
|
|
|
return $s; |
142
|
|
|
} // end of func GetLastContentType |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Match content to variables using preg |
147
|
|
|
* To read content currectly, content parsing is nesessary |
148
|
|
|
* Return value maybe string or array, use careful and |
149
|
|
|
* remind which value you use it for. |
150
|
|
|
* @param string $preg |
151
|
|
|
* @param string $str If obmitted, use $this->mRs |
152
|
|
|
* @return mixed |
153
|
|
|
* @see $mRs |
154
|
|
|
* @access public |
155
|
|
|
*/ |
156
|
|
|
public function Match($preg, $str = '') |
157
|
|
|
{ |
158
|
|
|
if (empty($preg)) return ''; |
159
|
|
|
if (empty($str)) |
160
|
|
|
$str = &$this->mRs; |
161
|
|
|
$i = preg_match_all($preg, $str, $ar, PREG_SET_ORDER); |
162
|
|
View Code Duplication |
if (0 == $i || false === $i) |
|
|
|
|
163
|
|
|
// Got none match or Got error |
164
|
|
|
$ar = ''; |
165
|
|
|
elseif (1 == $i) |
166
|
|
|
{ |
167
|
|
|
// Got 1 match, return as string or array(2 value in 1 match) |
168
|
|
|
$ar = $ar[0]; |
169
|
|
|
array_shift($ar); |
170
|
|
|
if (1 == count($ar)) |
171
|
|
|
$ar = $ar[0]; |
172
|
|
|
} |
173
|
|
|
else |
174
|
|
|
{ |
175
|
|
|
// Got more than 1 match return array contains string or sub-array |
176
|
|
|
foreach ($ar as &$row) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
array_shift($row); |
179
|
|
|
if (1 == count($row)) |
180
|
|
|
$row = $row[0]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
return $ar; |
184
|
|
|
} // end of func Match |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Http post content from host |
189
|
|
|
* |
190
|
|
|
* @param string $url Host address |
191
|
|
|
* @param mixed $param Post parameter, can be string or array. |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function Post ($url, $param = '') { |
195
|
|
|
curl_setopt($this->mSh, CURLOPT_POST, true); |
196
|
|
|
|
197
|
|
|
// Parse param, convert array to string |
198
|
|
|
if (is_array($param)) { |
199
|
|
|
$s = ''; |
200
|
|
|
foreach ($param as $key=>$val) |
201
|
|
|
$s .= "$key=$val&"; |
202
|
|
|
$param = $s; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
curl_setopt($this->mSh, CURLOPT_POSTFIELDS, $param); |
206
|
|
|
curl_setopt($this->mSh, CURLOPT_URL, $url); |
207
|
|
|
$this->mRs = curl_exec($this->mSh); |
208
|
|
|
|
209
|
|
|
if (0 != curl_errno($this->mSh)) |
210
|
|
|
$this->Log(curl_error($this->mSh), 4); |
211
|
|
|
|
212
|
|
|
return $this->mRs; |
213
|
|
|
} // end of func Post |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set some common options using curl_setopt |
218
|
|
|
* @access public |
219
|
|
|
*/ |
220
|
|
|
public function SetoptCommon () { |
221
|
|
|
$this->SetoptCookie(); |
222
|
|
|
$this->SetoptUseragent('ff14'); |
223
|
|
|
|
224
|
|
|
curl_setopt($this->mSh, CURLOPT_AUTOREFERER, true); |
225
|
|
|
// If got http error, report. |
226
|
|
|
curl_setopt($this->mSh, CURLOPT_FAILONERROR, true); |
227
|
|
|
|
228
|
|
|
// CURLOPT_FOLLOWLOCATION cannot set when open_basedir is set. |
229
|
|
|
// Also safe_mode, which are DEPRECATED in 5.3.0 and REMOVED in 5.4.0. |
230
|
|
|
if ('' == ini_get('open_basedir')) |
231
|
|
|
curl_setopt($this->mSh, CURLOPT_FOLLOWLOCATION, true); |
232
|
|
|
|
233
|
|
|
// Return result restead of display it. |
234
|
|
|
curl_setopt($this->mSh, CURLOPT_RETURNTRANSFER, true); |
235
|
|
|
curl_setopt($this->mSh, CURLOPT_CONNECTTIMEOUT, 300); |
236
|
|
|
curl_setopt($this->mSh, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
237
|
|
|
curl_setopt($this->mSh, CURLOPT_MAXREDIRS, 10); |
238
|
|
|
curl_setopt($this->mSh, CURLOPT_TIMEOUT, 300); |
239
|
|
|
|
240
|
|
|
// Accept all supported encoding(identity, deflate, gzip) |
241
|
|
|
// See CURLOPT_ACCEPT_ENCODING in libcurl |
242
|
|
|
// Set this to get uncompressed html content |
243
|
|
|
curl_setopt($this->mSh, CURLOPT_ENCODING, ''); |
244
|
|
|
|
245
|
|
|
curl_setopt($this->mSh, CURLOPT_SSL_CIPHER_LIST, 'TLSv1'); |
246
|
|
|
} // end of func SetoptCommon |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set cookie option |
251
|
|
|
* |
252
|
|
|
* If filename is not given, use default, |
253
|
|
|
* If file is given, use & set it as default. |
254
|
|
|
* |
255
|
|
|
* @param string $cookiefile |
256
|
|
|
* @access public |
257
|
|
|
*/ |
258
|
|
|
public function SetoptCookie ($cookiefile = '') { |
259
|
|
|
if (!empty($cookiefile)) |
260
|
|
|
$this->mCookiefile = $cookiefile; |
261
|
|
|
// /dev/null is useless cookie file, so does empty filename |
262
|
|
|
if (!empty($this->mCookiefile) |
263
|
|
|
&& ('/dev/null' != $this->mCookiefile)) { |
264
|
|
|
curl_setopt($this->mSh, CURLOPT_COOKIEFILE, $this->mCookiefile); |
265
|
|
|
curl_setopt($this->mSh, CURLOPT_COOKIEJAR, $this->mCookiefile); |
266
|
|
|
} |
267
|
|
|
} // end of func SetoptCookie |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set proxy option |
272
|
|
|
* @param int $ptype 0-no proxy, 1-http, 2-socks5 |
273
|
|
|
* @param string $phost |
274
|
|
|
* @param int $pport |
275
|
|
|
* @param string $pauth [username]:[password] |
276
|
|
|
* @access public |
277
|
|
|
*/ |
278
|
|
|
public function SetoptProxy($ptype, $phost, $pport, $pauth = '') |
279
|
|
|
{ |
280
|
|
|
if (0 == $ptype) { |
281
|
|
|
// Some server refuse http proxy tunnel, it's useless settings. |
282
|
|
|
//curl_setopt($this->mSh, CURLOPT_HTTPPROXYTUNNEL, false); |
283
|
|
|
} else { |
284
|
|
|
//curl_setopt($this->mSh, CURLOPT_HTTPPROXYTUNNEL, true); |
285
|
|
|
curl_setopt($this->mSh, CURLOPT_PROXY, $phost); |
286
|
|
|
if (1 == $ptype) |
287
|
|
|
curl_setopt($this->mSh, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
288
|
|
|
if (2 == $ptype) |
289
|
|
|
curl_setopt($this->mSh, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
290
|
|
|
curl_setopt($this->mSh, CURLOPT_PROXYPORT, $pport); |
291
|
|
|
if (!empty($pauth)) |
292
|
|
|
curl_setopt($this->mSh, CURLOPT_PROXYUSERPWD, $pauth); |
293
|
|
|
} |
294
|
|
|
} // end of func SetoptProxy |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Set http referer url |
299
|
|
|
* @param string $url |
300
|
|
|
*/ |
301
|
|
|
public function SetoptReferer($url) |
302
|
|
|
{ |
303
|
|
|
if (!empty($url)) |
304
|
|
|
curl_setopt($this->mSh, CURLOPT_REFERER, $url); |
305
|
|
|
} // end of func SetoptReferer |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Enable or disable ssl verify functin |
310
|
|
|
* Ssl verify is enabled by curl in default |
311
|
|
|
* |
312
|
|
|
* @param boolean $en True to enable, false to disable |
313
|
|
|
*/ |
314
|
|
|
public function SetoptSslverify ($en = true) { |
315
|
|
|
if (false === $en) { |
316
|
|
|
curl_setopt($this->mSh, CURLOPT_SSL_VERIFYPEER, false); |
317
|
|
|
curl_setopt($this->mSh, CURLOPT_SSL_VERIFYHOST, false); |
318
|
|
|
} |
319
|
|
|
} // end of func SetoptSslverify |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set browser agent option |
324
|
|
|
* @param string $browser |
325
|
|
|
* @access public |
326
|
|
|
*/ |
327
|
|
|
public function SetoptUseragent ($browser) { |
328
|
|
|
$b['ff14'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14'; |
|
|
|
|
329
|
|
|
$b['ie6'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'; |
330
|
|
|
$b['googlebot'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; |
331
|
|
|
|
332
|
|
|
if (isset($b[$browser])) |
333
|
|
|
curl_setopt($this->mSh, CURLOPT_USERAGENT, $b[$browser]); |
334
|
|
|
} // end of func SetoptUseragent |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
} // end of class Curl |
338
|
|
|
?> |
|
|
|
|
339
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.