function classic_curl($urls, $delay) {
$queue = curl_multi_init();
$map = array(); foreach ($urls as $url) {
// create cURL resources
$ch = curl_init(); // set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true); // add handle
curl_multi_add_handle($queue, $ch);
$map[$url] = $ch;
} $active = null; // execute the handles
do {
$mrc = curl_multi_exec($queue, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active > 0 && $mrc == CURLM_OK) {
if (curl_multi_select($queue, 0.5) != -1) {
do {
$mrc = curl_multi_exec($queue, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
} $responses = array();
foreach ($map as $url=>$ch) {
$responses[$url] = callback(curl_multi_getcontent($ch), $delay);
curl_multi_remove_handle($queue, $ch);
curl_close($ch);
} curl_multi_close($queue);
return $responses;
}

function rolling_curl($urls, $delay) {
$queue = curl_multi_init();
$map = array(); foreach ($urls as $url) {
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true); curl_multi_add_handle($queue, $ch);
$map[(string) $ch] = $url;
} $responses = array();
do {
while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ; if ($code != CURLM_OK) { break; } // a request was just completed -- find out which one
while ($done = curl_multi_info_read($queue)) { // get the info and content returned on the request
$info = curl_getinfo($done['handle']);
$error = curl_error($done['handle']);
$results = callback(curl_multi_getcontent($done['handle']), $delay);
$responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results'); // remove the curl handle that just completed
curl_multi_remove_handle($queue, $done['handle']);
curl_close($done['handle']);
} // Block for data in / output; error handling is done by curl_multi_exec
if ($active > 0) {
curl_multi_select($queue, 0.5);
} } while ($active); curl_multi_close($queue);
return $responses;
}

cURL: PHP并发处理方式的更多相关文章

  1. file_get_contents和curl对于post方式的解决办法

    post方式解决办法 其实很简单,我们只要仔细看看就知道了... file_get_contents: $content=$_POST['content'];$access_token=$_POST[ ...

  2. php curl 转为 x-www-form-urlencoded 方式的坑

    网上转变的方法基本都是写添加下面这句: curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-fo ...

  3. 表单提交 curl和浏览器方式

    表单被提交时,每个表单域都会被Url编码之后才在被发送. 浏览器每次向服务器发送url时,会进行编码,然后web服务器再进行解码. 所以,理论上,curl模拟登陆时,所传参数都必须urlencode一 ...

  4. curl使用post方式访问Spring Cloud gateway报time out错误

    公司老的项目使用是php,要进行重构.其他团队使用php curl函数使用post方式调用Spring Cloud gateway 报time out错误. 但是使用postman测试是没有任何问题, ...

  5. cocos2dx 网络编程(CCHttpRequest和CURL两个方式)

    转自:http://blog.csdn.net/sg619262284/article/details/20144087 在使用之前需要设置一些参数:参考:http://blog.csdn.net/w ...

  6. cURL模拟POST方式提交数据

    curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...

  7. 封装一个使用cURL以POST方式请求https协议的公众方法

    打开php7.2手册,搜索curl function getRequest($url,$type='get', $data = [], $timeout = 10) (需要更改){ $ssl = st ...

  8. curl post请求方式

    curl -l -H "application/x-www-form-urlencoded; charset=UTF-8" -X POST -d "query=SELEC ...

  9. php curl 转为 x-www-form-urlencoded 方式

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); fr ...

随机推荐

  1. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

    ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...

  2. poj 3469

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 18120   Accepted: 7818 ...

  3. js-jQuery对象与dom对象相互转换

    http://blog.csdn.net/jueshengtianya/article/details/8823091   核心提示:jquery选择器得到的jquery对象和标准的 javascri ...

  4. WebSocket 是什么原理?为什么可以实现持久连接

    你可以把 WebSocket 看成是 HTTP 协议为了支持长连接所打的一个大补丁,它和 HTTP 有一些共性,是为了解决 HTTP 本身无法解决的某些问题而做出的一个改良设计.在以前 HTTP 协议 ...

  5. java理论基础学习二

    JAVA开发工具 文本编辑器 UltraEdit EditPlus notepad++ 集成开发环境 IDE Integrated Development Enviroment JBuilder  h ...

  6. Sold out

    When will the writer see the play? 'The play may begin at any moment,'I said. 'It may have begun alr ...

  7. jqGrid中实现radiobutton的两种做法

    http://blog.sina.com.cn/s/blog_4f925fc30102e27j.html   jqGrid中实现radiobutton的两种做法 ------------------- ...

  8. java jms

    这篇博文我们主要介绍J2EE中的一个重要规范JMS,因为这个规范在企业中的应用十分的广泛,也比较重要,我们主要介绍JMS的基本概念和它的模式,消息的消费以及JMS编程步骤. 基本概念 JMS是java ...

  9. awk除去重复行

    awk去除重复行,思路是以每一行的$0为key,创建一个hash数组,后续碰到的行,如果数组里已经有了,就不再print了,否则将其print 测试文件: 用awk: 用sort+uniq好像出错了: ...

  10. android+apimonitor+genymotion

    1. 安装genymotion: http://www.genymotion.net/ 2. 设置使用adb Setting--adb--选择sdk的目录 3. apimonitor https:// ...