怎么了? MAMP下的php,mysql,apache: 一切都很好!但是当这个脚本正在执行时(即使只有1-3个链接),当前站点的整个页面请求被暂停(冻结)。多卷曲完成后,整个冻结页面请求立即加载。
class Web_Processor {
protected $conf;
/** @var \Web_Response[]*/
protected $response = array();
public function __construct(array $urls, Web_Configurator &$conf){//multi thread
$this->conf = &$conf;
$_autoreferrer = Kohana::$config->load('admin_/webloader.curl_autoreferer-X-val');
$_follow_loc = Kohana::$config->load('admin_/webloader.curl_followlocation-X-val');
$_max_redirects = Kohana::$config->load('admin_/webloader.curl_maxredirs-X-val');
$_connection_timeout = Kohana::$config->load('admin_/webloader.curl_connectiontimeout-X-val');
$_curl_timeout = Kohana::$config->load('admin_/webloader.curl_timeout-X-val');
//
$_autoreferrer = (bool) $_autoreferrer; //true/false
$_follow_loc = (bool) $_follow_loc; //true/false
$_max_redirects = (int) $_max_redirects; //5
$_connection_timeout = (int) $_connection_timeout; //20
$_curl_timeout = (int) $_curl_timeout; //25
$cmh = curl_multi_init();
// multicurl tasks
$tasks = array();
foreach ($urls as $url) {
// thread
$ch = curl_init($url); //URL
curl_setopt($ch, CURLOPT_AUTOREFERER, $_autoreferrer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $_follow_loc);
curl_setopt($ch, CURLOPT_MAXREDIRS, $_max_redirects);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //FIX "HTTPS://"
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $_connection_timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $_curl_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HTTP_CODE, true);
curl_setopt($ch, CURLOPT_PROXY, $this->conf->getProxyIP().":".$this->conf->getProxyPort()); //'xxx.xxx.xxx.xxx' : 'xxx'
curl_setopt($ch, CURLOPT_USERAGENT, $this->conf->getUserAgent()); //Opera/9.80 (Windows NT 5.1; U; en) Presto/2.9.168 Version/11.51
if($referrer = $this->conf->getReferrer() !== false){ //REFERRER
curl_setopt($ch, CURLOPT_REFERER, $referrer);
}
// thread descriptior to tasks array
$tasks[$url] = $ch;
// thread description to multicurl
curl_multi_add_handle($cmh, $ch);
}
// active threads count
$active = null;
// thread execution
do {
$mrc = curl_multi_exec($cmh, $active);
}
while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && ($mrc == CURLM_OK)) {
if (curl_multi_select($cmh) != -1) {
do {
$mrc = curl_multi_exec($cmh, $active);
$info = curl_multi_info_read($cmh);
if ($info['msg'] == CURLMSG_DONE) {
$ch = $info['handle'];
$url = array_search($ch, $tasks);
// getting content
$tasks[$url] = curl_multi_getcontent($ch);
$this->response[] = new Web_Response(
$url,
curl_getinfo($ch), //headers
curl_multi_getcontent($ch), //response body
curl_errno($ch), //error code
curl_error($ch) //error message
);
// deleting thread from multicurl
curl_multi_remove_handle($cmh, $ch);
// closing thread
curl_close($ch);
}
}
while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// closing multicurl
curl_multi_close($cmh);
}
public function results(){
return $this->response;
}
}