我创建了一个php脚本来运行一些谷歌查询,以便熟悉curl中多个并行请求的概念。作为基础,我在此页面上使用了示例#1:http://php.net/manual/en/function.curl-multi-exec.php
我发现提供的示例中的curl_multi_select总是返回-1。文档说明这表明发生了一些错误(通过底层系统调用),但似乎没有办法推断出错了。
$queries = array("Mr.", "must", "my", "name", "never", "new", "next", "no", "not", "now");
$mh = curl_multi_init();
$handles = array();
foreach($queries as $q)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.nl/#q=" . $q);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh,$ch);
$handles[] = $ch;
}
echo "created\n";
$active = null;
$mrc = curl_multi_exec($mh, $active);
if ($mrc != CURLM_OK)
throw new Exception("curl_multi_exec failed");
while ($active && $mrc == CURLM_OK) {
$select = curl_multi_select($mh);
if ($select != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
} else throw new Exception("curl_multi_select failed (it returned -1)");
}
// removed cleanup code for briefety.
如何找出为什么curl_multi_select返回-1或为什么返回-1?我是否需要一些特殊的php配置来允许这种多线程?
答案 0 :(得分:2)
正如评论http://php.net/manual/en/function.curl-multi-init.php#115055所说,官方文件存在问题。
我不知道为什么,也没有足够的libcurl知识,但我知道curl_multi_select($mh)
总是有机会返回-1;
所以,这个片段(来自上面的网址)对我有用。
<?php
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
?>