贝锐智能攀枝花建站部专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

php中curl异步并发请求http的示例

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

10年积累的成都网站设计、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有大祥免费网站建设让你可以放心的选择与我们合作。

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

$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/otherpic69/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/otherpic69/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>

            本文地址:<a href="http://mswzjz.cn/article/pdedjo.html">http://mswzjz.cn/article/pdedjo.html</a>

        </div>

    </div>

    <div class="other">

        <h3>其他资讯</h3>

        <ul>

            <li>

                    <a href="/article/dcoisjj.html">IP地址怎么查看对应域名端口 如何查ip对应的域名</a>

                </li><li>

                    <a href="/article/dcoiedd.html">帝国cms图片本地化大小 帝国cms图片显示不出来如何解决</a>

                </li><li>

                    <a href="/article/dcoieoo.html">腾讯云域名怎么认证 腾讯云域名使用教程</a>

                </li><li>

                    <a href="/article/dcoiedj.html">包含sap系统F_51清账的词条</a>

                </li><li>

                    <a href="/article/dcoisho.html">域名怎么绑定电脑 域名怎么绑定自家电脑</a>

                </li>
        </ul>

    </div>

</div>

<footer>

    <div class="message">

        <div class="mess container">

            <p>免费获取网站建设与品牌策划方案报价</p>

            <span>*主要业务范围包括:高端网站建设, 集团网站建设(网站建设网站制作)找网站建设公司就上四川攀枝花网站建设。</span>

            <form action="">

                <input type="text" class="ipt1" placeholder="联系人">

                <input type="text" class="ipt2" placeholder="联系电话">

                <textarea name="" id=""  placeholder="内容描述:描述您的需求,如网站、微信、电商、APP等。"></textarea>

                <a href="">提交需求</a>

            </form>

        </div>

    </div>

    <div class="footA">

        <div class="footAs container">

            <ul>

                <h3>联系我们</h3>

                <b>028-86922220</b>

                <li>手机:13518219792</li>

                <li>地址:成都市太升南路288号锦天国际A幢1002号</li>

                <li class="hr1"></li>

                <li>24小时服务热线:400-028-6601</li>

            </ul>

            <ul>

                <h3>网站建设服务</h3>

                <li>网页设计</li>

                <li>网站制作</li>

                <li>网站开发</li>

            </ul>

            <ul>

                <h3>网站推广服务</h3>

                <li>营销网站建设</li>

                <li>百度快速排名</li>

                <li>整站网站推广</li>

            </ul>

            <ul>

                <h3>网站运维服务</h3>

                <li>基础维护</li>

                <li>网站改版</li>

                <li>网站维护</li>

            </ul>

            <ul>

                <h3>FOLLOW US</h3>

                <li class="hr2"></li>

                <li>

                    <dd class="fl"><img src="/Public/Home/img/ewm.png" alt=""><p>微信二维码</p></dd>

                    <dd class="fr"><img src="/Public/Home/img/ewm.png" alt=""><p>微信二维码</p></dd>

                </li>

            </ul>

        </div>

        <div class="link container">

            友情链接:

            <a href="http://m.cdcxhl.com/" title="成都做网站" target="_blank">成都做网站</a>   <a href="http://m.cdcxhl.cn/qiye/
" title="企业网站建设公司" target="_blank">企业网站建设公司</a>   <a href="http://www.whjierui.cn/" title="乐山企业网站设计" target="_blank">乐山企业网站设计</a>   <a href="http://www.75091.cn/" title="德信企业管理" target="_blank">德信企业管理</a>   <a href="http://www.cqcxhl.com/service/ds.html" title="重庆电商网站建设" target="_blank">重庆电商网站建设</a>   <a href="https://www.cdcxhl.com/google.html" title="Google外贸推广" target="_blank">Google外贸推广</a>   <a href="http://www.cdxwcx.cn/tuoguan/zigong.html" title="自贡托管服务器" target="_blank">自贡托管服务器</a>   <a href="http://www.hyjierui.cn/" title="hyjierui.cn" target="_blank">hyjierui.cn</a>   <a href="http://www.cxjianzhan.com/" title="网站建设公司" target="_blank">网站建设公司</a>   <a href="https://www.scvps.cn/" title="网站域名" target="_blank">网站域名</a>   
        </div>

    </div>

    <div class="footB">

        <div class="container">

            <div class="fl">

                四川攀枝花网站建设公司     四川攀枝花网站建设公司-选我们!四川专业的攀枝花网站制作公司!

            </div>

            <div class="fr">

                All Rights Reserved 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备2024097831号-1</a>

            </div>

        </div>

    </div>

</footer>

</body>

</html>

<script>

    $(".con img").each(function(){

        var src = $(this).attr("src");    //获取图片地址

        var str=new RegExp("http");

        var result=str.test(src);

        if(result==false){

            var url = "https://www.cdcxhl.com"+src;    //绝对路径

            $(this).attr("src",url);

        }

    });

    window.onload=function(){

        document.oncontextmenu=function(){

            return false;

        }

    }

</script>