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. sematext

    https://sematext.atlassian.net/wiki/display/PUBLOGSENE/Syslog

  2. Windows服务创建及安装

    Windows服务创建及安装http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html

  3. JDBC第一次学习

     JDBC(Java Data Base Connectivity,java数据库连接),由一些类和接口构成的API,它是J2SE的一部分,由java.sql,javax.sql包组成. 应用程序.J ...

  4. 小奇模拟赛9.13 by hzwer

    2015年9月13日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿(explo) [题目背景] 小奇要开采一些矿物,它驾驶着一台带有钻头(初始能力值w)的飞船,按既定路线依次飞 ...

  5. 分布式 Key-Value 存储系统:Cassandra 入门

    Apache Cassandra 是一套开源分布式 Key-Value 存储系统.它最初由 Facebook 开发,用于储存特别大的数据. Cassandra 不是一个数据库,它是一个混合型的非关系的 ...

  6. Spring框架学习之第2节

    传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...

  7. android-exploitme(五):不安全的数据存储

    今天我来看看如果android将数据存储在sdcard,它的权限是什么样的 1. 打开emm软件,做一笔转账.

  8. Android 代码检查工具SonarQube

    http://blog.csdn.net/rain_butterfly/article/details/42170601 代码检查工具能帮我们检查一些隐藏的bug,代码检查工具中sonar是比较好的一 ...

  9. WebStorm 对 Mocha 完美支持。

    如果你在使用 Mocha 的话,请使用 WebStorm 来运行 mocha,Webstorm可以清晰查看每个 test 的详细信息,非常直观,强烈推荐.

  10. GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format

    下载相应的lib,编译安装就行了 cd ~ #下载包 wget http://www.imagemagick.org/download/delegates/zlib-1.2.7.tar.gz wget ...