php中curl异步并发请求http的示例-创新互联

小编给大家分享一下php中curl异步并发请求http的示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

目前创新互联公司已为上千家的企业提供了网站建设、域名、网页空间、网站运营、企业网站设计、揭东网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

$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 src="/upload/otherpic41/2207.jpg" alt="php中curl异步并发请求http的示例"></p><p>最下面可以看到时间花了27秒。</p><p>接下来看下php curl 异步并发请求http的代码以及花费时间。</p><pre>$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 src="/upload/otherpic41/2208.jpg" alt="php中curl异步并发请求http的示例"></p><p>才花了3秒?实际上我感觉应该是花了5秒,因为启动比同步要慢,开始的时候卡了2秒。</p><p>http请求效率,毋庸置疑是异步远远高于同步。</p><p>核心请求代码如下:(这是老外写的,有点小问题,最后的提示undefined offset)</p><pre>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>修改一下。只要在新增url的时候加个判断就好了。// 当$i等于$urls数组大小时不用再增加了。</p><pre>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>以上是“php中curl异步并发请求http的示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!</p>                <br>
                本文标题:php中curl异步并发请求http的示例-创新互联                <br>
                URL标题:<a href="http://myzitong.com/article/dddeoc.html">http://myzitong.com/article/dddeoc.html</a>
            </div>
        </div>
        <div class="contentr fr">
            <h3>其他资讯</h3>
            <ul>
                <li>
                        <a href="/article/cjjeeo.html">全球50家企业内部源代码遭外泄</a>
                    </li><li>
                        <a href="/article/cjjsjh.html">VirtualBox虚拟机网络设置(四种方式)</a>
                    </li><li>
                        <a href="/article/cjjeci.html">中小企业在现阶段建设网站的必要性</a>
                    </li><li>
                        <a href="/article/cjjsee.html">如何快速掌握交互设计基础</a>
                    </li><li>
                        <a href="/article/cjjeec.html">建设企业网站对企业的重要性有哪些?</a>
                    </li>            </ul>
        </div>
    </div>
</div>
<!--底部-->
<footer>
    <div class="foot">
        <div class="container">
            <h1>梓潼大橙子建站您身边的网站制作服务商</h1>
            <div class="foot1">
                <ul>
                    <li>
                        <dl><i class="iconfont"></i><b>地址ADDRESS</b></dl>
                        <p>四川-成都青羊区太升南路288号<br>
                            锦天国际A座10楼
                        </p>
                    </li>
                    <li>
                        <dl><i class="iconfont"></i><b>电话/TEL</b></dl>
                        <p><a href="tel:02886922220" target="_blank">028 86922220</a> (工作日)<br>
                            <a href="tel:18980820575" target="_blank">1898082 0575</a> ( 7x24 )
                        </p>
                    </li>
                    <li>
                        <dl><i class="iconfont"></i><b>QQ咨询</b></dl>
                        <p> 244261566 (售前)<br>
                            631063699 (售后)
                        </p>
                    </li>
                    <li>
                        <dl><i class="iconfont"></i><b>邮箱/E: mail</b></dl>
                        <p> service@cdcxhl.com (业务)<br>
                            hr@cdcxhl.com (求职)
                        </p>
                    </li>
                </ul>
            </div>
            <div class="link">
                友情链接:
                <a href="https://www.cdcxhl.com/h5.html" title="成都响应式网站" target="_blank">成都响应式网站</a>   <a href="https://www.cdcxhl.com/xiyun.html" title="西云服务器托管" target="_blank">西云服务器托管</a>   <a href="http://www.ybwzjz.com/" title="宜宾网站设计" target="_blank">宜宾网站设计</a>   <a href="http://chengdu.cdcxhl.com/mb/" title="成都模板网站" target="_blank">成都模板网站</a>   <a href="https://www.cdcxhl.com/idc/mintian.html" title="珉田数据中心" target="_blank">珉田数据中心</a>   <a href="http://www.cdkjz.cn/fangan/store/" title="电商网站建设方案" target="_blank">电商网站建设方案</a>   <a href="http://www.scyucang.com/" title="宇仓仓储" target="_blank">宇仓仓储</a>   <a href="https://www.cdcxhl.com/mianfei/zuo/chengdu.html" title="成都免费做网站公司" target="_blank">成都免费做网站公司</a>   <a href="https://www.cdcxhl.com/yingxiao.html" title="成都营销网站建设" target="_blank">成都营销网站建设</a>   <a href="http://www.cxjianzhan.com/mobile/" title="手机网站开发" target="_blank">手机网站开发</a>               </div>
        </div>
    </div>
    <div class="copy container">
        Copyright