Home>Article>Backend Development> About php curl asynchronous concurrent request http

About php curl asynchronous concurrent request http

藏色散人
藏色散人 forward
2021-01-06 16:30:06 5471browse

Recommended: "PHP Video Tutorial"

Let’s first look at the synchronization code and request time.

$start_time=date("h:i:sa"); for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html"; GetTitle(geturl("http://www.downxia.com/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('/.*<\/title>/i',$output,$matches); var_dump($matches[0]); } $end_time=date("h:i:sa"); echo '开始时间是:'.$start_time; echo '结束时间是:'.$end_time;</pre>
      <p><img alt="" class="has" src="//m.sbmmt.com/img/upload/article/000/000/020/f3308981971ab2270c03dc7d2f94fb4b-0.jpg"></p>
      <p>You can see at the bottom that the time took 27 seconds.</p>
      <p>Next, let’s take a look at the code and time spent on php curl’s asynchronous concurrent http requests.</p>
      <pre class="brush:php;toolbar:false;">$start_time=date("h:i:sa"); $urls=[]; for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html"; } var_dump($urls); // GetTitle('klasjdkla<title>313asds12'); rolling_curl($urls,'GetTitle'); function GetTitle($output){ preg_match('/.*<\/title>/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't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => 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 (it's important to do this before removing the old one) $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 completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }</pre>
      <p><img alt="" class="has" src="//m.sbmmt.com/img/upload/article/000/000/020/f3308981971ab2270c03dc7d2f94fb4b-1.jpg"></p>
      <p>It only took 3 seconds? In fact, I think it took 5 seconds, because startup is slower than synchronization, and it was stuck for 2 seconds at the beginning.</p>
      <p>http request efficiency, there is no doubt that asynchronous is much higher than synchronous.</p>
      <p>The core request code is as follows: (This is written by a foreigner, there is a slight problem, the last prompt is undefined offset)</p>
      <pre class="brush:php;toolbar:false;">function rolling_curl($urls, $callback, $custom_options = null) {//多个url访问 // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => 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 (it's important to do this before removing the old one) $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 completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }</pre>
      <p>Modify it. Just add a judgment when adding a new URL. // When $i is equal to the size of the $urls array, there is no need to increase it.</p>
      <pre class="brush:php;toolbar:false;">function rolling_curl($urls, $callback, $custom_options = null) {//多个url访问 // make sure the rolling window isn't greater than the # of urls $rolling_window = 5; $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window; $master = curl_multi_init(); $curl_arr = array(); // add additional curl options here $std_options = array( CURLOPT_RETURNTRANSFER => 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 (it's 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 completed curl_multi_remove_handle($master, $done['handle']); } else { // request failed. add error handling. } } } while ($running); curl_multi_close($master); return true; }</pre>
      <p>Above, the end. Write it down so you don’t forget.</p>
      <p>The above is the detailed content of About php curl asynchronous concurrent request http. For more information, please follow other related articles on the PHP Chinese website!</p>
     </div>
     <div class="nphpQianMsg">
      <div class="clear"></div>
     </div>
     <div class="nphpQianSheng">
      <span>Statement:</span>
      <div>
       This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
      </div>
     </div>
    </div>
    <div class="nphpSytBox">
     <span>Previous article:<a class="dBlack" title="Detailed explanation of PHP bit operators" href="//m.sbmmt.com/m/faq/468001.html">Detailed explanation of PHP bit operators</a></span>
     <span>Next article:<a class="dBlack" title="Detailed explanation of PHP bit operators" href="//m.sbmmt.com/m/faq/468210.html">Detailed explanation of PHP bit operators</a></span>
    </div>
    <div class="nphpSytBox2">
     <div class="nphpZbktTitle">
      <h2>Related articles</h2>
      <em><a href="//m.sbmmt.com/m/article.html" class="bBlack"><i>See more</i><b></b></a></em>
      <div class="clear"></div>
     </div>
     <ul class="nphpXgwzList">
      <li><b></b><a href="//m.sbmmt.com/m/faq/462969.html" title="How to install curl extension for php" class="aBlack">How to install curl extension for php</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/463284.html" title="How to solve php curl garbled problem" class="aBlack">How to solve php curl garbled problem</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/467322.html" title="About PHP CURL uploading binary stream images" class="aBlack">About PHP CURL uploading binary stream images</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/467770.html" title="Introduction to ubuntu php7.1 installation curl" class="aBlack">Introduction to ubuntu php7.1 installation curl</a>
       <div class="clear"></div></li>
     </ul>
    </div>
   </div>
   <div class="nphpFoot">
    <div class="nphpFootBg">
     <ul class="nphpFootMenu">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><p>Home</p></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><p>Course</p></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li>
      <li><a href="//m.sbmmt.com/m/login"><b class="icon5"></b><p>My</p></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpYouBox" style="display: none;">
    <div class="nphpYouBg">
     <div class="nphpYouTitle">
      <span onclick="$('.nphpYouBox').hide()"></span>
      <a href="//m.sbmmt.com/m/"></a>
      <div class="clear"></div>
     </div>
     <ul class="nphpYouList">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><span>Home</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><span>Course</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/article.html"><b class="icon3"></b><span>Article</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><span>Q&A</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/dic.html"><b class="icon6"></b><span>Dictionary</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course/type/99.html"><b class="icon7"></b><span>Manual</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/xiazai/"><b class="icon8"></b><span>Download</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/faq/zt" title=""><b class="icon12"></b><span>Topic</span>
        <div class="clear"></div></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpDing" style="display: none;">
    <div class="nphpDinglogo">
     <a href="//m.sbmmt.com/m/"></a>
    </div>
    <div class="nphpNavIn1">
     <div class="swiper-container nphpNavSwiper1">
      <div class="swiper-wrapper">
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/">Home</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/article.html" class="hover">Article</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/wenda.html">Q&A</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/course.html">Course</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/faq/zt">Topic</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/xiazai">Download</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/game">Game</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/dic.html">Dictionary</a>
       </div>
       <div class="clear"></div>
      </div>
     </div>
     <div class="langadivs">
      <a href="javascript:;" class="bg4 bglanguage"></a>
      <div class="langadiv">
       <a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a>
       <a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a>
       <a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a>
       <a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a>
       <a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a>
       <a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a>
       <a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a>
       <a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a>
      </div>
     </div>
    </div>
   </div>
   <!--顶部导航 end-->
  </div>
  <link rel="stylesheet" id="_main-css" href="//m.sbmmt.com/m/static/css/viewer.min.css" type="text/css" media="all">
 </body>
</html>