快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

php通过curl多线程抓取网站内容

自php5.0开始,增加了如下是curl支持多线程的函数
 

curl_multi_init - initialize a new cURL multi handle.
             It will return the cURL handle on success and FALSE on error.
curl_multi_add_handle — Add a cURL handle to a cURL multi handle.
curl_multi_exec — Runs all the curl handle in the cURL multi handle in parallel.
curl_multi_remove_handle — Removes a cURL handle from a cURL multi handle.
curl_multi_close — close the cURL multi handle.
 


弄了个简单的例子
 

class MultiHttpRequest{
        public $urls = array();
        public $curlopt_header = 1;
        public $method = "GET";

        function __construct($urls = false)
        {
                $this->urls = $urls;
        }

        function set_urls($urls)
        {
                $this->urls = $urls;
                return $this;
        }

        function is_return_header($b)
        {
                $this->curlopt_header = $b;
                return $this;
        }

        function set_method($m)
        {
                $this->medthod = strtoupper($m);
                return $this;
        }

        function start()
        {
                if(!is_array($this->urls) or count($this->urls) == 0){
                        return false;
                }

                $curl = $text = array();

                $handle = curl_multi_init();

                foreach($this->urls as $k=>$v){
                        $curl[$k] = $this->add_handle($handle, $v);
                }

                $this->exec_handle($handle);

                foreach($this->urls as $k=>$v){
                        $text[$k] =  curl_multi_getcontent ($curl[$k]);
                        echo $text[$k], "\n\n";
                        curl_multi_remove_handle($handle, $curl[$k]);
                }

                curl_multi_close($handle);
        }
顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论