【发布时间】:2013-10-28 15:33:26
【问题描述】:
我需要一个接一个地向同一个域发出多个 curl 请求,但不能并行。
我在http://technosophos.com/找到了以下代码示例
这在加速重复的 curl 调用方面效果很好。
function get2($url) {
// Create a handle.
$handle = curl_init($url);
// Set options...
// Do the request.
$ret = curlExecWithMulti($handle);
// Do stuff with the results...
// Destroy the handle.
curl_close($handle);
}
function curlExecWithMulti($handle) {
// In real life this is a class variable.
static $multi = NULL;
// Create a multi if necessary.
if (empty($multi)) {
$multi = curl_multi_init();
}
// Add the handle to be processed.
curl_multi_add_handle($multi, $handle);
// Do all the processing.
$active = NULL;
do {
$ret = curl_multi_exec($multi, $active);
} while ($ret == CURLM_CALL_MULTI_PERFORM);
while ($active && $ret == CURLM_OK) {
if (curl_multi_select($multi) != -1) {
do {
$mrc = curl_multi_exec($multi, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// Remove the handle from the multi processor.
curl_multi_remove_handle($multi, $handle);
return TRUE;
}
我已经多次尝试通过设置 curl 选项来获取函数 curlExecWithMulti($handle) 以将 curl 的结果作为变量返回,但到目前为止没有成功。
这个可以吗?
【问题讨论】:
-
什么意思?你做了
return TRUE;,那么除了TRUE,你期望得到什么? -
抱歉,这只是来自 technosophos.com 的代码示例供参考。我试图返回 $ret 值,但无法让它工作
-
您期望获得什么价值。检查这个php.net/manual/en/function.curl-multi-exec.php