天天看點

php curl批處理--可控并發異步通常情況下 PHP 中的 cURL 是阻塞運作的,就是說建立一個 cURL 請求以後必須等它執行成功或者逾時才會執行下一個請求:API接口通路一般會首選CURL

在實際項目或者自己編寫小工具(比如新聞聚合,商品價格監控,比價)的過程中, 通常需要從第3方網站或者api接口擷取資料, 在需要處理1個url隊列時, 為了提高性能, 可以采用curl提供的curl_multi_*族函數實作簡單的并發.

php curl批處理--可控并發異步通常情況下 PHP 中的 cURL 是阻塞運作的,就是說建立一個 cURL 請求以後必須等它執行成功或者逾時才會執行下一個請求:API接口通路一般會首選CURL

<?php  

include 'curl.class.php';  

function callback($response, $info, $error, $request)  

{  

    echo 'response:<br>';  

    print_r($response);  

    echo '<br>' . date("y-m-d h:i:s") . '   <br>';  

    echo '<br>' . str_repeat("-", 100) . '<br>';  

}  

$user_cookie = (!empty($_request['cookie'])) ? $_request['cookie'] : file_get_contents("cookie.txt");  

$curl = new curl ("callback");  

$data = array(  

    array(  

        'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qmr&type=rec_gametime&referfrom=&rt=0.42521539455332336', //秦美人  

        'method' => 'post',  

        'post_data' => '',  

        'header' => null,  

        'options' => array(  

            curlopt_referer => "http://niu.xunlei.com/entergame/?gameno=qmr&fenqunum=3",  

            curlopt_cookie => $user_cookie,  

        )  

    ),  

        'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=sq&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神曲  

            curlopt_referer => "http://niu.xunlei.com/entergame/?gameno=sq&fenqunum=41",  

        'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=frxz&type=rec_gametime&referfrom=&rt=0.42521539455332336', //凡人修真  

            curlopt_referer => "http://niu.xunlei.com/entergame/?gameno=frxz&fenqunum=3",  

        'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=smxj&type=rec_gametime&referfrom=&rt=0.42521539455332336', //神魔仙界  

            curlopt_referer => "http://niu.xunlei.com/entergame/?gameno=smxj&fenqunum=2",  

        'url' => 'http://dyactive2.vip.xunlei.com/com_sign/?game=qsqy&type=rec_gametime&referfrom=&rt=0.42521539455332336', //傾世情緣  

            curlopt_referer => "http://niu.xunlei.com/entergame/?gameno=qsqy&fenqunum=11",  

);  

foreach ($data as $val) {  

    $request = new curl_request ($val ['url'], $val ['method'], $val ['post_data'], $val ['header'], $val ['options']);  

    $curl->add($request);  

$curl->execute();  

echo $curl->display_errors();  

使用下來效果很好,沒有副作用,并發數可控,應用之處多多,自己發揮想象吧

php curl批處理--可控并發異步通常情況下 PHP 中的 cURL 是阻塞運作的,就是說建立一個 cURL 請求以後必須等它執行成功或者逾時才會執行下一個請求:API接口通路一般會首選CURL

/** 

 * curl批量處理  工具類 

 *  

 * @since version 1.0 

 * @author justmepzy <[email protected]> 

 * @link http://t.qq.com/justpzy 

 */  

 *單一的請求對象 

class curl_request {  

    public $url         = '';  

    public $method      = 'get';  

    public $post_data   = null;  

    public $headers     = null;  

    public $options     = null;  

    /** 

     *  

     * @param string $url 

     * @param string $method 

     * @param string $post_data 

     * @param string $headers 

     * @param array $options 

     * @return void 

     */  

    public function __construct($url, $method = 'get', $post_data = null, $headers = null, $options = null) {  

        $this->url = $url;  

        $this->method = strtoupper( $method );  

        $this->post_data = $post_data;  

        $this->headers = $headers;  

        $this->options = $options;  

    }  

    public function __destruct() {  

        unset ( $this->url, $this->method, $this->post_data, $this->headers, $this->options );  

 * 包含請求列隊處理 

class curl {  

     * 請求url個數 

     * @var int 

    private $size           = 5;  

     * 等待所有curl批進行中的活動連接配接等待響應時間 

    private $timeout        = 5;  

     * 完成請求回調函數 

     * @var string 

    private $callback       = null;  

     * crul配置 

     * @var array 

    private $options        = array (curlopt_ssl_verifypeer => 0,curlopt_returntransfer => 1,curlopt_connecttimeout => 30 );  

     * 請求頭 

    private $headers        = array ();  

     * 請求列隊 

    private $requests       = array ();  

     * 請求列隊索引 

    private $request_map    = array ();  

     * 錯誤 

    private $errors         = array ();  

     * @access public 

     * @param string $callback 回調函數 

     * 該函數有4個參數($response,$info,$error,$request) 

     * $response    url傳回的body 

     * $info        curl連接配接資源句柄的資訊 

     * $error       錯誤 

     * $request     請求對象 

    public function __construct($callback = null) {  

        $this->callback = $callback;  

     * 添加一個請求對象到列隊 

     * @param object $request 

     * @return boolean 

    public function add($request) {  

        $this->requests [] = $request;  

        return true;  

     * 建立一個請求對象并添加到列隊 

    public function request($url, $method = 'get', $post_data = null, $headers = null, $options = null) {  

        $this->requests [] = new curl_request ( $url, $method, $post_data, $headers, $options );  

     * 建立get請求對象 

    public function get($url, $headers = null, $options = null) {  

        return $this->request ( $url, "get", null, $headers, $options );  

     * 建立一個post請求對象 

    public function post($url, $post_data = null, $headers = null, $options = null) {  

        return $this->request ( $url, "post", $post_data, $headers, $options );  

     * 執行curl 

     * @param int $size 最大連接配接數 

     * @return ambigous <boolean, mixed>|boolean 

    public function execute($size = null) {  

        if (sizeof ( $this->requests ) == 1) {  

            return $this->single_curl ();  

        } else {  

            return $this->rolling_curl ( $size );  

        }  

     * 單個url請求 

     * @access private 

     * @return mixed|boolean 

    private function single_curl() {  

        $ch = curl_init ();  

        $request = array_shift ( $this->requests );  

        $options = $this->get_options ( $request );  

        curl_setopt_array ( $ch, $options );  

        $output = curl_exec ( $ch );  

        $info = curl_getinfo ( $ch );  

        // it's not neccesary to set a callback for one-off requests  

        if ($this->callback) {  

            $callback = $this->callback;  

            if (is_callable ( $this->callback )) {  

                call_user_func ( $callback, $output, $info, $request );  

            }  

        } else  

            return $output;  

     * 多個url請求 

    private function rolling_curl($size = null) {  

        if ($size)  

            $this->size = $size;  

        else   

            $this->size = count($this->requests);  

        if (sizeof ( $this->requests ) < $this->size)  

            $this->size = sizeof ( $this->requests );  

        if ($this->size < 2)  

            $this->set_error ( 'size must be greater than 1' );  

        $master = curl_multi_init ();  

        //添加curl連接配接資源句柄到map索引  

        for($i = 0; $i < $this->size; $i ++) {  

            $ch = curl_init ();  

            $options = $this->get_options ( $this->requests [$i] );  

            curl_setopt_array ( $ch, $options );  

            curl_multi_add_handle ( $master, $ch );  

            $key = ( string ) $ch;  

            $this->request_map [$key] = $i;  

        $active = $done = null;  

        do {  

            while ( ($execrun = curl_multi_exec ( $master, $active )) == curlm_call_multi_perform )  

                ;  

            if ($execrun != curlm_ok)  

                break;  

            //有一個請求完成則回調  

            while ( $done = curl_multi_info_read ( $master ) ) {  

                //$done 完成的請求句柄  

                $info = curl_getinfo ( $done ['handle'] );//  

                $output = curl_multi_getcontent ( $done ['handle'] );//  

                $error = curl_error ( $done ['handle'] );//  

                $this->set_error ( $error );  

                //調用回調函數,如果存在的話  

                $callback = $this->callback;  

                if (is_callable ( $callback )) {  

                    $key = ( string ) $done ['handle'];  

                    $request = $this->requests [$this->request_map [$key]];  

                    unset ( $this->request_map [$key] );  

                    call_user_func ( $callback, $output, $info, $error, $request );  

                }  

                curl_close ( $done ['handle'] );  

                //從列隊中移除已經完成的request  

                curl_multi_remove_handle ( $master, $done ['handle'] );  

            //等待所有curl批進行中的活動連接配接  

            if ($active)  

                curl_multi_select ( $master, $this->timeout );  

        } while ( $active );  

        //完成關閉  

        curl_multi_close ( $master );  

     * 擷取沒得請求對象的curl配置 

     * @return array 

    private function get_options($request) {  

        $options = $this->__get ( 'options' );  

        if (ini_get ( 'safe_mode' ) == 'off' || ! ini_get ( 'safe_mode' )) {  

            $options [curlopt_followlocation] = 1;  

            $options [curlopt_maxredirs] = 5;  

        $headers = $this->__get ( 'headers' );  

        if ($request->options) {  

            $options = $request->options + $options;  

        $options [curlopt_url] = $request->url;  

        if ($request->post_data && strtolower($request->method) == 'post' ) {  

            $options [curlopt_post] = 1;  

            $options [curlopt_postfields] = $request->post_data;  

        if ($headers) {  

            $options [curlopt_header] = 0;  

            $options [curlopt_httpheader] = $headers;  

        return $options;  

     * 設定錯誤資訊 

     * @param string $msg 

    public function set_error($msg) {  

        if (! empty ( $msg ))  

            $this->errors [] = $msg;  

     * 擷取錯誤資訊 

     * @param string $open 

     * @param string $close 

     * @return string 

    public function display_errors($open = '<p>', $close = '</p>') {  

        $str = '';  

        foreach ( $this->errors as $val ) {  

            $str .= $open . $val . $close;  

        return $str;  

     * @param string $name 

     * @param string $value 

    public function __set($name, $value) {  

        if ($name == 'options' || $name == 'headers') {  

            $this->{$name} = $value + $this->{$name};  

            $this->{$name} = $value;  

     * @return mixed 

    public function __get($name) {  

        return (isset ( $this->{$name} )) ? $this->{$name} : null;  

        unset ( $this->size, $this->timeout, $this->callback, $this->options, $this->headers, $this->requests, $this->request_map, $this->errors );  

// end curl class  

/* end of file curl.class.php */