基于PHP CURL用法的深入分析
更新時間:2013年06月09日 11:18:04 作者:
本篇文章是對PHP中的CURL用法進行了詳細(xì)的分析介紹,需要的朋友參考下
如下所示:
<?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);
?>
復(fù)制代碼 代碼如下:
<?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 CURL獲取cookies模擬登錄的方法
- php使用curl模擬登錄后采集頁面的例子
- php cURL和Rolling cURL并發(fā)方式比較
- 使用PHP curl模擬瀏覽器抓取網(wǎng)站信息
- PHP CURL CURLOPT參數(shù)說明(curl_setopt)
- php中通過curl檢測頁面是否被百度收錄
- php curl基本操作詳解
- 解析php中curl_multi的應(yīng)用
- php curl獲取網(wǎng)頁內(nèi)容(IPV6下超時)的解決辦法
- php curl選項列表(超詳細(xì))
- 關(guān)于php 接口問題(php接口主要也就是運用curl,curl函數(shù))
- 解析PHP 使用curl提交json格式數(shù)據(jù)
- 解析php擴展php_curl.dll不加載的解決方法
- PHP的curl實現(xiàn)get,post和cookie(實例介紹)
- 深入PHP curl參數(shù)的詳解
- 解析php curl_setopt 函數(shù)的相關(guān)應(yīng)用及介紹
- 基于PHP CURL獲取郵箱地址的詳解
- PHP Curl多線程原理實例詳解
相關(guān)文章
php封裝db類連接sqlite3數(shù)據(jù)庫的方法實例
sqlite數(shù)據(jù)庫只用一個文件就ok,小巧方便,所以是一個非常不錯的嵌入式數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于php利用封裝db類連接sqlite3的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
php中使用array_filter()函數(shù)過濾數(shù)組實例講解
在本篇文章里小編給大家分享的是一篇關(guān)于php中使用array_filter()函數(shù)過濾數(shù)組實例講解,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
Mac M1安裝mnmp(Mac+Nginx+MySQL+PHP)開發(fā)環(huán)境
這篇文章主要介紹了Mac M1安裝mnmp(Mac+Nginx+MySQL+PHP)開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
PHP使用mysqli操作MySQL數(shù)據(jù)庫的簡單方法
下面小編就為大家?guī)硪黄狿HP使用mysqli操作MySQL數(shù)據(jù)庫的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

