彭山网站建设设计

将想法与焦点和您一起共享

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>
            分享链接:<a href="http://ncjierui.cn/article/dddeoc.html">http://ncjierui.cn/article/dddeoc.html</a>
        </div>
    </div>
    <div class="news_r">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/giisp.html">如何在Vue中不使用router-link方法实现一个页面跳转功能-创新互联</a>
                </li><li>
                    <a href="/article/giise.html">如何让css图片居中显示-创新互联</a>
                </li><li>
                    <a href="/article/giiej.html">在notepad++实现代码自动化排版的示例-创新互联</a>
                </li><li>
                    <a href="/article/giisd.html">iOS下一键调试Push的示例分析-创新互联</a>
                </li><li>
                    <a href="/article/giiep.html">Linux—Shell检查哪些主机开启了匿名FPT服务-创新互联</a>
                </li>        </ul>
    </div>
</div>
<!--尾部begin-->
<!--尾部begin-->
<footer>
    <div class="f_bg">
        <div class="wrap">
            <div class="links">
                <h2 class="h2">美图云海建站解决方案<a href="../solution/" title="更多" class="more">更多+</a></h2>
                <ul>
                    <li><a href="../solution/xiaochengxu.html" title="小程序定制解决方案">小程序定制解决方案</a></li>
                    <li><a href="../solution/qiyewz.html" title="企业网站建设解决方案">企业网站建设解决方案</a></li>
                    <li><a href="../solution/menhuwz.html" title="行业门户网站建设解决方案">行业门户网站建设解决方案</a></li>
                    <li><a href="../solution/yingxiaowz.html" title="营销型网站建设解决方案">营销型网站建设解决方案</a></li>
                    <li><a href="../solution/waimaowz.html" title="外贸网站建设解决方案">外贸网站建设解决方案</a></li>
                    <li><a href="../solution/pingpaiwz.html" title="品牌形象网站建设解决方案">品牌形象网站建设解决方案</a></li>
                    <li><a href="../solution/dianziwz.html" title="数码、电子产品网站建设解决方案">数码、电子产品网站建设解决方案</a></li>
                    <li><a href="../solution/jituanwz.html" title="集团、上市企业网站建设解决方案">集团、上市企业网站建设解决方案</a></li>
                    <li><a href="../solution/dichanwz.html" title="房地产、地产项目网站建设解决方案">房地产、地产项目网站建设解决方案</a></li>
                    <li><a href="../solution/zhubaowz.html" title="珠宝高端奢侈品网站建设解决方案">珠宝高端奢侈品网站建设解决方案</a></li>
                </ul>
            </div>
            <div class="links w2">
                <h2 class="h2">我们的实力<a href="../about/" title="更多" class="more">更多+</a></h2>
                <ul>
                    <li>10年专业互联网服务经验</li>
                    <li>彭山高端建站设计团队</li>
                    <li>资深行业分析策划</li>
                    <li>B2C营销型网站建设者</li>
                    <li>前沿视觉设计、研发能力</li>
                    <li>前端代码深度符合SEO优化</li>
                    <li>彭山市高新技术企业证书</li>
                    <li>具有完备的项目管理</li>
                    <li>完善的售后服务体系</li>
                    <li>深厚的网络运营经验</li>
                    <li>时刻新技术研发能力</li>
                    <li>擅长网站设计营销推广</li>
                </ul>
            </div>
            <div class="f_div2_r">
                <h2 class="h2">关于美图云海网站建设<a href="../about/" title="更多" class="more">更多+</a></h2>
                美图云海网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,美图云海网站建设网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计... </div>
            <div class="c_l"></div>
        </div>
        <div class="wrap">
            <div class="link">
                友情链接:
                <a href="http://www.njfdjwx.com/" title="成都许可证办理" target="_blank">成都许可证办理</a>   <a href="http://www.mzlewan.com/" title="成都亚克力加工" target="_blank">成都亚克力加工</a>   <a href="http://www.ahjierui.cn/" title="ahjierui.cn" target="_blank">ahjierui.cn</a>   <a href="https://www.cdxwcx.com/wangzhan/app.html" title="手机app" target="_blank">手机app</a>   <a href="http://www.yafdjwx.com/" title="项目咨询" target="_blank">项目咨询</a>   <a href="http://www.cdkjz.cn/fangan/response/" title="响应式网站设计方案" target="_blank">响应式网站设计方案</a>   <a href="http://m.cdxwcx.com/xcx.html" title="成都小程序开发" target="_blank">成都小程序开发</a>   <a href="http://www.gtmaxf.cn/" title="消防维保" target="_blank">消防维保</a>   <a href="http://www.cxhljz.cn/solution/" title="教育建设方案" target="_blank">教育建设方案</a>   <a href="http://m.xwcx.net/" title="响应式报价" target="_blank">响应式报价</a>               </div>
        </div>
        <div class="wrap">
            <div class="f_div3"> <span class="l">彭山网站制作案例©2025 青羊区美图云海设计工作室(个体工商户)  彭山建站美图云海工作室 版权所有  | <a href="http://www.ncjierui.cn" target="_blank">彭山网站设计</a><a href="http://www.ncjierui.cn" target="_blank">www.ncjierui.cn</a></span> <span class="r"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备2025119305号-1</a></span> </div>
        </div>
    </div>
</footer>
<!--尾部end-->
<script language="javascript" src="/Public/Home/js/foot.js"></script>
<!--尾部end-->
<!--侧边栏begin-->
<div class="side">
    <ul>
        <li id="qqonline_xbceo"><a href="tencent://message/?uin=631063699&Site=&Menu=yes"><i class="bgs1"></i>QQ咨询</a></li>
        <li class="shangqiao"><a href="tencent://message/?uin=532337155&Site=&Menu=yes" title="在线咨询">
            <div><i class="bgs2"></i>在线咨询</div>
        </a></li>
        <li class="sideewm"><i class="bgs3"></i>官方微信
            <div class="ewBox"></div>
        </li>
        <li class="sideetel"><i class="bgs4"></i>联系电话
            <div class="telBox">
                <dd class="bgs1"><span>座机</span><a href="tel:028-86922220" target="_blank">028-86922220</a></dd>
                <dd class="bgs2"><span>手机</span><a href="tel:13518219792" target="_blank">13518219792</a></dd>
            </div>
        </li>
        <li class="sidetop" onClick="goTop()" id="sidetop"><i class="bgs6"></i>返回顶部</li>
    </ul>
</div>
<script type="text/javascript">
    $('.sideewm').hover(function(){
        $('.ewBox').stop().fadeIn();
    },function(){
        $('.ewBox').stop().fadeOut();
    });
    $('.sideetel').hover(function(){
        $('.telBox').stop().fadeIn();
    },function(){
        $('.telBox').stop().fadeOut();
    });
</script>
<!-- WPA start -->
<!-- WPA end -->
<!--侧边栏end-->
</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>