PHP cURL multi_exec请求之间的延迟 [英] PHP cURL multi_exec delay between requests

查看:1119
本文介绍了PHP cURL multi_exec请求之间的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行一个标准的cURL_multi_exec函数(下面的例子),我得到所有cURL句柄请求一次。我想在每个请求之间延迟100ms,是否有办法做到这一点? (在Google& StackOverflow搜索中找不到)

If I run a standard cURL_multi_exec function (example below), I get all cURL handles requested at once. I would like to put a delay of 100ms between each request, is there a way to do that? (nothing found on Google & StackOverflow search)

我在curl_multi_exec()之前尝试过usleep(),它减慢了脚本,但不推迟每个请求。 p>

I've tried usleep() before curl_multi_exec() which slows down the script but does not postpone each request.

// array of curl handles & results
$curlies = array();
$result = array();
$mh = curl_multi_init();

// setup curl requests
for ($id = 0; $id <= 10; $id += 1) {
    $curlies[$id] = curl_init();
    curl_setopt($curlies[$id], CURLOPT_URL,            "http://google.com");
    curl_setopt($curlies[$id], CURLOPT_HEADER,         0);
    curl_setopt($curlies[$id], CURLOPT_RETURNTRANSFER, 1);
    curl_multi_add_handle($mh, $curlies[$id]);
}

// execute the handles
$running = null;
do {
    curl_multi_exec($mh, $running);
} while($running > 0);

// get content and remove handles
foreach($curlies as $id => $c) {
    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
}

// all done
curl_multi_close($mh);

我在这一整天工作,任何帮助将非常感谢!非常感谢。

I'm working on this all day, any help would be greatly appreciated! Thank you.

编辑:任何其他非cUrl方法?这也会回答我的问题。

Any other non-cUrl method? That would also answer my question.

推荐答案

不要以为你可以。如果您从cli运行此脚本,您可以将您的脚本转换为 10进程,然后从每个进程打开常规curl请求。这将允许您对时间进行精细控制。

Don't think you can. If you run this from the cli, you could instead fork your script into 10 processes and then fire regular curl requests from each. That would allow you fine grained control over the timing.

这篇关于PHP cURL multi_exec请求之间的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆