您好,欢迎访问一九零五行业门户网

巧用curl 并发减少后端访问时间

转载自:http://www.phpskill.com/html/show-1-4403-1.html php经验分享交流群:323899029,这里面有更多精彩文章 在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时50
转载自:http://www.phpskill.com/html/show-1-4403-1.html
php经验分享交流群:323899029,这里面有更多精彩文章
在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时500毫 秒那么我们三个接口就要花费1500毫秒了,这个问题太头疼了严重影响了页面访问速度,有没有可能并发访问来提高速度呢?今天就简单的说一下,利用 curl并发来提高页面访问速度, php 1、老的curl访问方式以及耗时统计0) { $data = false; } curl_close($ch); return $data;}function microtime_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_arr as $key=>$val) { $data[$key]=curl_fetch($val); } $time_end = microtime_float(); $time = $time_end - $time_start; echo 耗时:{$time};?>耗时:0.614秒
2、curl并发访问方式以及耗时统计 $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 ($urlarr as $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, );function microtime_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};?>
耗时:0.316秒帅气吧整个页面访问后端接口的时间节省了一半php经验分享交流群:323899029,这里面有更多精彩文章3、curl相关参数curl_close — close a curl sessioncurl_copy_handle — copy a curl handle along with all of its preferencescurl_errno — return the last error numbercurl_error — return a string containing the last error for the current sessioncurl_exec — perform a curl sessioncurl_getinfo — get information regarding a specific transfercurl_init — initialize a curl sessioncurl_multi_add_handle — add a normal curl handle to a curl multi handlecurl_multi_close — close a set of curl handlescurl_multi_exec — run the sub-connections of the current curl handlecurl_multi_getcontent — return the content of a curl handle if curlopt_returntransfer is setcurl_multi_info_read — get information about the current transferscurl_multi_init — returns a new curl multi handlecurl_multi_remove_handle — remove a multi handle from a set of curl handlescurl_multi_select — wait for activity on any curl_multi connectioncurl_setopt_array — set multiple options for a curl transfercurl_setopt — set an option for a curl transfercurl_version — gets curl version information 转载自:http://www.phpskill.com/html/show-1-4403-1.htmlphp经验分享交流群:323899029,这里面有更多精彩文章
其它类似信息

推荐信息