cURL: PHP并发处理方式
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并发处理方式的更多相关文章
- file_get_contents和curl对于post方式的解决办法
post方式解决办法 其实很简单,我们只要仔细看看就知道了... file_get_contents: $content=$_POST['content'];$access_token=$_POST[ ...
- php curl 转为 x-www-form-urlencoded 方式的坑
网上转变的方法基本都是写添加下面这句: curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-fo ...
- 表单提交 curl和浏览器方式
表单被提交时,每个表单域都会被Url编码之后才在被发送. 浏览器每次向服务器发送url时,会进行编码,然后web服务器再进行解码. 所以,理论上,curl模拟登陆时,所传参数都必须urlencode一 ...
- curl使用post方式访问Spring Cloud gateway报time out错误
公司老的项目使用是php,要进行重构.其他团队使用php curl函数使用post方式调用Spring Cloud gateway 报time out错误. 但是使用postman测试是没有任何问题, ...
- cocos2dx 网络编程(CCHttpRequest和CURL两个方式)
转自:http://blog.csdn.net/sg619262284/article/details/20144087 在使用之前需要设置一些参数:参考:http://blog.csdn.net/w ...
- cURL模拟POST方式提交数据
curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...
- 封装一个使用cURL以POST方式请求https协议的公众方法
打开php7.2手册,搜索curl function getRequest($url,$type='get', $data = [], $timeout = 10) (需要更改){ $ssl = st ...
- curl post请求方式
curl -l -H "application/x-www-form-urlencoded; charset=UTF-8" -X POST -d "query=SELEC ...
- php curl 转为 x-www-form-urlencoded 方式
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); fr ...
随机推荐
- vc++ 获取当前用户名
#include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)
题目链接:http://acm.upc.edu.cn/problem.php?id=2224 题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A< ...
- iOS第三方(ActionSheet)-JTSActionSheet
外观和系统的基本一样 github地址:https://github.com/jaredsinclair/JTSActionSheet 百度云下载: http://pan.baidu.com/s/1q ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...
- mmap 的理解
mmap 的理解 采用共享内存通信的一个显而易见的好处 是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存 ...
- threadlocal精髓是为每一个线程保证一个共享对象,保证一个,保证是同一个
threadlocal精髓是为每一个线程保证一个共享对象,保证一个,保证同一个线程中是同一个共享对象. 如果是静态变量是共享的话,那必须同步,否则尽管有副本,还是会出错,故C错
- VS2010/MFC编程入门之四(MFC应用程序框架分析)
VS2010/MFC编程入门之四(MFC应用程序框架分析)-软件开发-鸡啄米 http://www.jizhuomi.com/software/145.html 上一讲鸡啄米讲的是VS2010应用 ...
- CentOS 加载/挂载 U盘
1.以root用户登陆 先加载USB模块 modprobe usb-storage 用fdisk -l 看看U盘的设备 假如U盘是sda1 2.确定在 目录 /mnt 下建立了 文件夹 ...
- Hibernate的配置文件解析
配置mybatis.xml或hibernate.cfg.xml报错: <property name="connection.url">jdbc:mysql://loca ...