我需要检查网站的一些代理,所以我使用Curl多接口同时进行。使用现有的示例和提示,我编写了这段代码:
public function process(){
// *** Multi handle initialized and easy handles added
$active = null;
do {
$mrc = curl_multi_exec($this->cmh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && ($mrc == CURLM_OK)){
if (curl_multi_select($this->cmh, 0.1) == -1) {
usleep(100);
}
do {
// stucked here
$mrc = curl_multi_exec($this->cmh, $active);
while (false != ($mhinfo = curl_multi_info_read($this->cmh))){
if ($mhinfo['msg'] == CURLMSG_DONE) {
$handle = $mhinfo['handle'];
$result = $mhinfo['result'];
// *** Result analysis
curl_multi_remove_handle($this->cmh, $handle);
curl_close($handle);
}
}
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
}
if ($mrc != CURLM_OK)
throw new CurlError ($this, $this->cmh, $mrc, curl_multi_strerror ($mrc));
// *** MULTI HANDLE WILL BE CLOSE LATER
}
在此处轻松处理:
public function getCurlHandle() {
$ch = curl_init ("https://www.avito.ru/");
$headers = array
(
'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8',
'Accept-Encoding: gzip, deflate, sdch'
);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_PROXY, (string)$this->proxy);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36");
return $ch;
}
问题是有时它会挂起curl_multi_exec并且会因超时而超时。有php版本和curl版本信息:
PHP 5.5.12-2ubuntu4.3 (cli) (built: Mar 16 2015 20:50:26)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
php > print_r (curl_version());
Array
(
[version_number] => 468225
[age] => 3
[features] => 50877
[ssl_version_number] => 0
[version] => 7.37.1
[host] => x86_64-pc-linux-gnu
[ssl_version] => OpenSSL/1.0.1f
[libz_version] => 1.2.8
[protocols] => Array
(
[0] => dict
[1] => file
[2] => ftp
[3] => ftps
[4] => gopher
[5] => http
[6] => https
[7] => imap
[8] => imaps
[9] => ldap
[10] => ldaps
[11] => pop3
[12] => pop3s
[13] => rtmp
[14] => rtsp
[15] => smtp
[16] => smtps
[17] => telnet
[18] => tftp
)
)