基于PHP CURL用法的深入分析
程序员文章站
2022-04-08 15:56:54
如下所示:复制代码 代码如下:
如下所示:
<?php
header('context-type:text/html;charset:gb2312;');
$urls = array(
'http://www.baidu.com/',
'http://www.pconline.com.cn/',
'http://www.163.com/'
);
$options = array(
curlopt_returntransfer=>1,
curlopt_followlocation=>1,
curlopt_header => false,
curlopt_httpheader => array(
'accept'=>' text/html, application/xhtml+xml,',
'accept-encoding'=>' gzip, deflate',
'accept-language'=>' zh-cn',
'connection'=>' keep-alive',
'user-agent'=>' mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0)',
),
);
function curlmultirequest($urls,$options=array()){
$ch = array();
$results = array();
$mh = curl_multi_init();
foreach($urls as $key=>$val){
$ch[$key] = curl_init();
if($options){
curl_setopt_array($ch[$key],$options);
}
curl_setopt($ch[$key],curlopt_url,$val);
curl_multi_add_handle($mh,$ch[$key]);
}
$running = null;
do{
curl_multi_exec($mh,$running);
}while($running>0);
foreach($ch as $key=>$val){
//$results[$key] = iconv('gb2312','utf-8',curl_multi_getcontent($val));
$results[$key] = curl_multi_getcontent($val);
curl_multi_remove_handle($mh,$val);
curl_close($val);
}
curl_multi_close($mh);
return $results;
}
$results = curlmultirequest($urls,$options);
print_r($results);
?>
复制代码 代码如下:
<?php
header('context-type:text/html;charset:gb2312;');
$urls = array(
'http://www.baidu.com/',
'http://www.pconline.com.cn/',
'http://www.163.com/'
);
$options = array(
curlopt_returntransfer=>1,
curlopt_followlocation=>1,
curlopt_header => false,
curlopt_httpheader => array(
'accept'=>' text/html, application/xhtml+xml,',
'accept-encoding'=>' gzip, deflate',
'accept-language'=>' zh-cn',
'connection'=>' keep-alive',
'user-agent'=>' mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0)',
),
);
function curlmultirequest($urls,$options=array()){
$ch = array();
$results = array();
$mh = curl_multi_init();
foreach($urls as $key=>$val){
$ch[$key] = curl_init();
if($options){
curl_setopt_array($ch[$key],$options);
}
curl_setopt($ch[$key],curlopt_url,$val);
curl_multi_add_handle($mh,$ch[$key]);
}
$running = null;
do{
curl_multi_exec($mh,$running);
}while($running>0);
foreach($ch as $key=>$val){
//$results[$key] = iconv('gb2312','utf-8',curl_multi_getcontent($val));
$results[$key] = curl_multi_getcontent($val);
curl_multi_remove_handle($mh,$val);
curl_close($val);
}
curl_multi_close($mh);
return $results;
}
$results = curlmultirequest($urls,$options);
print_r($results);
?>
上一篇: 解析dedecms空间迁移步骤详解