您好,欢迎访问一九零五行业门户网

使用 PHP 和 cURL 获取 URLs

http://css.dzone.com/articles/retrieving-urls-parallel-curl
class footo_content_retrieve_http_curlparallel{ /** * fetch a collection of urls in parallell using curl. the results are * returned as an associative array, with the urls as the key and the * content of the urls as the value. * * @param array $addresses an array of urls to fetch. * @return array the content of each url that we've been asked to fetch. **/ public function retrieve($addresses) { $multihandle = curl_multi_init(); $handles = array(); $results = array(); foreach($addresses as $url) { $handle = curl_init($url); $handles[$url] = $handle; curl_setopt_array($handle, array( curlopt_header => false, curlopt_returntransfer => true, )); curl_multi_add_handle($multihandle, $handle); } //execute the handles $result = curlm_call_multi_perform; $running = false; // set up and make any requests.. while ($result == curlm_call_multi_perform) { $result = curl_multi_exec($multihandle, $running); } // wait until data arrives on all sockets while($running && ($result == curlm_ok)) { if (curl_multi_select($multihandle) > -1) { $result = curlm_call_multi_perform; // while we need to process sockets while ($result == curlm_call_multi_perform) { $result = curl_multi_exec($multihandle, $running); } } } // clean up foreach($handles as $url => $handle) { $results[$url] = curl_multi_getcontent($handle); curl_multi_remove_handle($multihandle, $handle); curl_close($handle); } curl_multi_close($multihandle); return $results; }}
复制代码
其它类似信息

推荐信息