我正在尝试同时curl_multi_*
获取多个(大约 50 个)URL,因为连续这样做需要很长时间。但是,我在执行时收到以下错误消息curl_multi_add_handle
。
警告:(null)(): 10 不是第0行未知中的有效 cURL 句柄资源
这是代码:
//create the multiple cURL handle
$mh = curl_multi_init();
// Loop over pages and get set the URL to the cURL queue
foreach ($htmltogetlist as $source) {
[...]
// Get a cURL handle of the current URL
$urls[$id]['ch'] = $this->_getCurlHandle($urls[$id]['url']);
// Success
if (gettype($urls[$id]['ch']) == 'resource' && get_resource_type($urls[$id]['ch']) == 'curl') {
curl_multi_add_handle($mh, $urls[$id]['ch']); // << Produces error
}
}
并且$this->_getCurlHandle
有:
// Set cURL handle
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
return $ch;
奇怪的是这$urls[$id]['ch']
是一个有效resource
的类型curl
,但我不能将它添加到$mh
文件中,错误unknown
在line 0
. 实际上,我什至可以执行句柄并从中获得正确的响应,所以我肯定它是有效的。我只是无法将其添加到$mh
.
$content = curl_exec($urls[$id]['ch']);
$response = curl_getinfo($urls[$id]['ch']);
print_r($response); // Works
我知道 cURL 适用于多次传输。例如,下面的代码(本质上做同样的事情)有效。我也确定不是导致问题的 URL 数量,因为在将数据库查询限制为 1 时出现相同的错误:
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch1,CURLOPT_ENCODING , "gzip");
curl_setopt($ch1, CURLOPT_REFERER, $url);
curl_setopt($ch1, CURLOPT_USERAGENT, 'PHP');
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_AUTOREFERER, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2,CURLOPT_ENCODING , "gzip");
curl_setopt($ch2, CURLOPT_REFERER, $url);
curl_setopt($ch2, CURLOPT_USERAGENT, 'PHP');
curl_setopt($ch2, CURLOPT_HEADER, true);
curl_setopt($ch2, CURLOPT_AUTOREFERER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1); // Works
curl_multi_add_handle($mh,$ch2); // Works