利用curl并发来提高页面访问速度
在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时500毫 秒那么我们三个接口就要花费1500毫秒了,这个问题太头疼了严重影响了页面访问速度,有没有可能并发访问来提高速度呢?今天就简单的说一下,利用 curl并发来提高页面访问速度,
希望大家多指导。
1、老的curl访问方式以及耗时统计
functioncurl_fetch($url,$timeout=3){
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data= curl_exec($ch);
$errno= curl_errno($ch);
if($errno>0) {
$data= false;
}
curl_close($ch);
return$data;
}
functionmicrotime_float()
{
list($usec,$sec) =explode(" ", microtime());
return((float)$usec+ (float)$sec);
}
$url_arr=array(
"taobao"=>"http://www.taobao.com",
"sohu"=>"http://www.sohu.com",
"sina"=>"http://www.sina.com.cn",
);
$time_start= microtime_float();
$data=array();
foreach($url_arras$key=>$val)
{
$data[$key]=curl_fetch($val);
}
$time_end= microtime_float();
$time=$time_end-$time_start;
echo"耗时:{$time}";
?>
<?php
functioncurl_multi_fetch($urlarr=array()){
$result=$res=$ch=array();
$nch= 0;
$mh= curl_multi_init();
foreach($urlarras$nk=>$url) {
$timeout=2;
$ch[$nch] = curl_init();
curl_setopt_array($ch[$nch],array(
CURLOPT_URL =>$url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT =>$timeout,
));
curl_multi_add_handle($mh,$ch[$nch]);
++$nch;
}
/* wait for performing request */
do{
$mrc= curl_multi_exec($mh,$running);
}while(CURLM_CALL_MULTI_PERFORM ==$mrc);
while($running&&$mrc== CURLM_OK) {
// wait for network
if(curl_multi_select($mh, 0.5) > -1) {
// pull in new data;
do{
$mrc= curl_multi_exec($mh,$running);
}while(CURLM_CALL_MULTI_PERFORM ==$mrc);
}
}
if($mrc!= CURLM_OK) {
error_log("CURL Data Error");
}
/* get data */
$nch= 0;
foreach($urlarras$moudle=>$node) {
if(($err= curl_error($ch[$nch])) =='') {
$res[$nch]=curl_multi_getcontent($ch[$nch]);
$result[$moudle]=$res[$nch];
}
else
{
error_log("curl error");
}
curl_multi_remove_handle($mh,$ch[$nch]);
curl_close($ch[$nch]);
++$nch;
}
curl_multi_close($mh);
return$result;
}
$url_arr=array(
"taobao"=>"http://www.taobao.com",
"sohu"=>"http://www.sohu.com",
"sina"=>"http://www.sina.com.cn",
);
functionmicrotime_float()
{
list($usec,$sec) =explode(" ", microtime());
return((float)$usec+ (float)$sec);
}
$time_start= microtime_float();
$data=curl_multi_fetch($url_arr);
$time_end= microtime_float();
$time=$time_end-$time_start;
echo"耗时:{$time}";
?>
来自:http://cn2.php.net/manual/en/ref.curl.php
curl_close — Close a cURL session
curl_copy_handle — Copy a cURL handle along with all of its preferences
curl_errno — Return the last error number
curl_error — Return a string containing the last error for the current session
curl_exec — Perform a cURL session
curl_getinfo — Get information regarding a specific transfer
curl_init — Initialize a cURL session
curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
curl_multi_close — Close a set of cURL handles
curl_multi_exec — Run the sub-connections of the current cURL handle
curl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read — Get information about the current transfers
curl_multi_init — Returns a new cURL multi handle
curl_multi_remove_handle — Remove a multi handle from a set of cURL handles
curl_multi_select — Wait for activity on any curl_multi connection
curl_setopt_array — Set multiple options for a cURL transfer
curl_setopt — Set an option for a cURL transfer
curl_version — Gets cURL version information
利用curl并发来提高页面访问速度的更多相关文章
- 萧墙HTML5手机发展之路(51)——jquerymobile在提高页面访问速度
正在使用jQuery Mobile开发时间可以选择单页模板和多页模板,在单页模板时从一个页面跳转到另一个页面时从需要server要求.用户会感到轻微的停顿. 使用多页模板,为了改善网页之间跳跃的流畅, ...
- 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度。
巧用linux服务器的/dev/shm/ 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度. 首先让我们认识一下,什么是tmpfs和/dev/shm/? ...
- Javascript高性能编程-提高数据访问速度
hasOwnProperty()仅检索实例不检索原型,in即检索实例,又检索原型 成员嵌套越深,访问速度越慢,只在必要的情况下使用对象成员. 如果在同一个函数中你要多次读 ...
- 超简单!教你如何修改源列表(sources.list)来提高软件访问速度
因为Ubuntu官方的源地址不在国内,所以在国内的访问速度非常慢,比如:我们要下载或是更新软件那速度比蜗牛还慢.所以,我们需要改成国内的镜像服务器,这样,我们在下载或更新软件的时候就会很快了. 配置步 ...
- Javascript高性能编程-提高Dom访问速度
在浏览器中对于Dom的操作和普通的脚本的操作处于两个不同的dll中,两个dll的交互是比较耗时的,优化对Dom的操作可以提高脚本的执行速度.下面是对如何优化的一些总结: 将需要多次操作的节点存储在一个 ...
- 使用Gzip压缩数据,加快页面访问速度
在返回的json数据量大时,启用Gzip压缩,可以提高传输效率.下面为Gzip压缩对json字符串压缩并输出到页面的代码. 一.代码 /** 向浏览器输出字符串响应数据,启用 ...
- 小强的HTML5移动开发之路(51)——jquerymobile中改善页面访问速度
在使用jQuery Mobile进行开发的时候可以选择单页模版和多页模版,在使用单页模版的时候从一个页面跳转到另一个页面的时候需要从服务器请求,用户会感到略有停顿.使用多页模版,可以改善页面跳转之间的 ...
- Google Font字体本地化使用提高网站访问速度
Google Web font在国内经常不稳定,速度在国内延迟也很高,而引发网页打开速度慢. 一.常见的字体格式介绍 不同的浏览器对字体格式支持是不一致的,常见的如下: 1.TureTpe(.ttf) ...
- 利用curl抓取远程页面内容
最基本的操作如下 $curlPost = 'a=1&b=2';//模拟POST数据$cookie_file = tempnam('./temp','kie');//可选,保存ses ...
随机推荐
- Effective Project Communications
I was recently invited to speak at a conference in Singapore on Effective Project Communications. I' ...
- Mybatis在xml文件中处理大于号小于号的方法
第一种方法:用了转义字符把">"和"<"替换掉,然后就没有问题了. AND start_date <= CURRENT_DATE AND en ...
- iOS 开发者账号共用发布证书 (Distribution)问题
苹果客服回复: 1.第一台申请发布证书的电脑,从钥匙串中导出发布证书(Distribution)颁发的request文件?然后在第二台电脑上用request文件新生成一个Distribution证书, ...
- 输出Java的GC信息
-verbose:gc -XX:+printGC 可以打印GC的简要信息 [GC 4790K->374K(15872K), 0.0001606 secs] [GC 4790K->374K( ...
- iOS:crash崩溃日志分析
一.前言: 作为一个合格的iOS开发者,除了具有规范强悍的编码能力外,还应该具有过硬的查错纠错能力.在项目运行时,程序崩溃是不可避免的,遇到这个问题,有时会出现一大堆的crash日志,艹,貌似看不懂呀 ...
- AES对称加密算法原理
原著:James McCaffrey 翻译:小刀人 原文出处:MSDN Magazine November 2003 (Encrypt It) 本文的代码下载:msdnmag200311AES.exe ...
- Linux 使用 bg 和 fg 切换任务运行状态
将Linux任务转换到后台执行 平时在运行 Linux 任务时,在任务运行时又想运行其它任务,那么可以把任务切换到后台运行. 使用 ctrl+z 可以把当前的任务转入到后台,这时任务并没有被中止,可使 ...
- jquery.form插件
提交表单的2种方式 // ajaxForm $("#form1").ajaxForm(options); // ajaxSubmit $ ...
- 如何查看lib文件的导出函数
参考:http://blog.csdn.net/brioxu/article/details/6932350 dumpbin /exports /out:xxx.dmp xxx.lib
- String的replaceAll方法中的正则表达式用法
项目里面 需要对已手机号码进行 如下的显示 比如15088688388 要显示为150****8388的效果 实现这个简单的效果 方法有很多 我想试试用正则表达式去实现 查了点资料最终试出来以下方法可 ...