PHP可控制并发数的异步并发CURL

参考网址:http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/

封装的很好
用起来很方便~
但是注意哦~这个里面有bug,还有一些html需要完善
function rolling_curl ( $urls , $callback , $custom_options = null ) {

    // make sure the rolling window isn't greater than the # of urls
    $rolling_window = 5 ;
    $rolling_window = ( sizeof ( $urls ) &lt ; $rolling_window ) ? sizeof ( $urls ) : $rolling_window ;

    $master = curl_multi_init ( ) ;
    $curl_arr = array ( ) ;

    // add additional curl options here
    $std_options = array (CURLOPT_RETURNTRANSFER =&gt ; true ,
    CURLOPT_FOLLOWLOCATION =&gt ; true ,
    CURLOPT_MAXREDIRS =&gt ; 5 ) ;
    $options = ( $custom_options ) ? ( $std_options + $custom_options ) : $std_options ;

    // start the first batch of requests
    for ( $i = 0 ; $i &lt ; $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 ) ;
//bug在这里,他会有一次空请求
                // 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 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值