1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 关于php curl异步并发请求http

关于php curl异步并发请求http

时间:2022-11-20 17:22:07

相关推荐

关于php curl异步并发请求http

后端开发|php教程

php,curl

后端开发-php教程

有哪些好的信息分类网站源码,ubuntu终端操作技巧,tomcat如何建立服务器,c 爬虫代码,php留言功能的个人页面,seo网上引流lzw

推荐:《PHP视频教学》

软件授权管理网站源码,vscode 头文件提示,ubuntu 语言管理,tomcat6.0测试,pbi爬虫,php下载的文件打不开,唐山seo推广咨询热线,网游网站 源码下载,仿赶集网论坛模板lzw

先来看下同步的代码以及请求时间。

zint源码,Vscode扩展迁移,ubuntu ssbc,tomcat最新多少,sqlite3虚拟表,网页3d插件下载,人们常说的前端框架是什么,薄荷里面有白色爬虫吗,php跨域 header,小白自学seo 广告,直播网站开源项目,网页access,易企秀导入模板下载lzw

$start_time=date("h:i:sa");for ($i=0; $i <100 ; $i++) { $urls[]="/downinfo/2315".$i.".html";GetTitle(geturl("/downinfo/2315".$i.".html"));}function geturl($url){$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($ch); curl_close($ch); return $output;}function GetTitle($output){preg_match(/.*/i,$output,$matches);var_dump($matches[0]);}$end_time=date("h:i:sa");echo 开始时间是:.$start_time;echo 结束时间是:.$end_time;

最下面可以看到时间花了27秒。

接下来看下php curl 异步并发请求http的代码以及花费时间。

$start_time=date("h:i:sa");$urls=[];for ($i=0; $i <100 ; $i++) { $urls[]="/downinfo/2315".$i.".html";}var_dump($urls);// GetTitle(klasjdkla313asds12);rolling_curl($urls,GetTitle);function GetTitle($output){preg_match(/.*/i,$output,$matches);var_dump($matches[0]);}$end_time=date("h:i:sa");echo 开始时间是:.$start_time;echo 结束时间是:.$end_time;function rolling_curl($urls, $callback, $custom_options = null){//多个url访问 // make sure the rolling window isn greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS=> 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done[handle]); if ($info[http_code] == 200) {$output = curl_multi_getcontent($done[handle]);// request successful. process output using the callback function.$callback($output);// start a new request (its important to do this before removing the old one)$ch = curl_init();$options[CURLOPT_URL] = $urls[$i++]; // increment icurl_setopt_array($ch, $options);curl_multi_add_handle($master, $ch);// remove the curl handle that just completedcurl_multi_remove_handle($master, $done[handle]); } else {// request failed. add error handling. } } } while ($running); curl_multi_close($master); return true;}

才花了3秒?实际上我感觉应该是花了5秒,因为启动比同步要慢,开始的时候卡了2秒。

http请求效率,毋庸置疑是异步远远高于同步。

核心请求代码如下:(这是老外写的,有点小问题,最后的提示undefined offset)

function rolling_curl($urls, $callback, $custom_options = null){//多个url访问 // make sure the rolling window isn greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS=> 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done[handle]); if ($info[http_code] == 200) {$output = curl_multi_getcontent($done[handle]);// request successful. process output using the callback function.$callback($output);// start a new request (its important to do this before removing the old one)$ch = curl_init();$options[CURLOPT_URL] = $urls[$i++]; // increment icurl_setopt_array($ch, $options);curl_multi_add_handle($master, $ch);// remove the curl handle that just completedcurl_multi_remove_handle($master, $done[handle]); } else {// request failed. add error handling. } } } while ($running); curl_multi_close($master); return true;}

修改一下。只要在新增url的时候加个判断就好了。// 当$i等于$urls数组大小时不用再增加了。

function rolling_curl($urls, $callback, $custom_options = null){//多个url访问 // make sure the rolling window isn greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS=> 5 ); $options = ($custom_options) ? ($std_options + $custom_options) : $std_options; // start the first batch of requests for ($i = 0; $i < $rolling_window; $i++) { $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i]; curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch); } do { while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); if ($execrun != CURLM_OK) { break; } // a request was just completed -- find out which one while ($done = curl_multi_info_read($master)) { $info = curl_getinfo($done[handle]); if ($info[http_code] == 200) {$output = curl_multi_getcontent($done[handle]);// request successful. process output using the callback function.$callback($output);// start a new request (its important to do this before removing the old one)// 当$i等于$urls数组大小时不用再增加了if($i<sizeof($urls)){ $ch = curl_init(); $options[CURLOPT_URL] = $urls[$i++]; // increment i curl_setopt_array($ch, $options); curl_multi_add_handle($master, $ch);}// remove the curl handle that just completedcurl_multi_remove_handle($master, $done[handle]); } else {// request failed. add error handling. } } } while ($running); curl_multi_close($master); return true;}

以上,结束。记录一下,以免自己忘记。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。