比file_get_contents穩(wěn)定的curl_get_contents分享
更新時間:2012年01月11日 21:47:21 作者:
相信使用過file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問不了時,會導(dǎo)致頁面漫長的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個函數(shù)就誕生了
分享一個實(shí)際在用的函數(shù):
/*比file_get_contents穩(wěn)定的多!$timeout為超時時間,單位是秒,默認(rèn)為1s。*/
function curl_get_contents($url,$timeout=1) {
$curlHandle = curl_init();
curl_setopt( $curlHandle , CURLOPT_URL, $url );
curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
$result = curl_exec( $curlHandle );
curl_close( $curlHandle );
return $result;
}
$hx = curl_get_contents('http://m.fzitv.net');
相信使用過file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問不了時,會導(dǎo)致頁面漫長的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個函數(shù)就誕生了。curl的一些常識介紹
保留原file_get_contents函數(shù)的原因是當(dāng)讀取本地文件時,用原生的file_get_contents顯然更合適。
另來自張宴的file_get_contnets的優(yōu)化,具體可看:http://m.fzitv.net/article/28030.htm
同樣是設(shè)置超時時間來解決這個問題。如果沒裝curl,就必須得用這個方式了。
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1 //設(shè)置一個超時時間,單位為秒
)
)
);
file_get_contents("http://m.fzitv.net/", 0, $ctx);
另外,據(jù)不完全測試,使用curl獲取頁面比用file_get_contents穩(wěn)定的多。
復(fù)制代碼 代碼如下:
/*比file_get_contents穩(wěn)定的多!$timeout為超時時間,單位是秒,默認(rèn)為1s。*/
function curl_get_contents($url,$timeout=1) {
$curlHandle = curl_init();
curl_setopt( $curlHandle , CURLOPT_URL, $url );
curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
$result = curl_exec( $curlHandle );
curl_close( $curlHandle );
return $result;
}
$hx = curl_get_contents('http://m.fzitv.net');
相信使用過file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問不了時,會導(dǎo)致頁面漫長的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個函數(shù)就誕生了。curl的一些常識介紹
保留原file_get_contents函數(shù)的原因是當(dāng)讀取本地文件時,用原生的file_get_contents顯然更合適。
另來自張宴的file_get_contnets的優(yōu)化,具體可看:http://m.fzitv.net/article/28030.htm
同樣是設(shè)置超時時間來解決這個問題。如果沒裝curl,就必須得用這個方式了。
復(fù)制代碼 代碼如下:
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1 //設(shè)置一個超時時間,單位為秒
)
)
);
file_get_contents("http://m.fzitv.net/", 0, $ctx);
另外,據(jù)不完全測試,使用curl獲取頁面比用file_get_contents穩(wěn)定的多。
您可能感興趣的文章:
- php基于curl重寫file_get_contents函數(shù)實(shí)例
- PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題
- php中file_get_contents與curl性能比較分析
- php采用file_get_contents代替使用curl實(shí)例
- 深入file_get_contents與curl函數(shù)的詳解
- 探討file_get_contents與curl效率及穩(wěn)定性的分析
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP curl 或 file_get_contents 獲取需要授權(quán)頁面的方法
相關(guān)文章
遞歸刪除一個節(jié)點(diǎn)以及該節(jié)點(diǎn)下的所有節(jié)點(diǎn)示例
一段遞歸刪除一個部門以及它的所有子部門的一段代碼。僅供大家參考和自己以后備用2014-03-03
PHP實(shí)現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了php操作xml格式數(shù)據(jù)類的定義及簡單使用技巧,需要的朋友可以參考下2018-03-03
php結(jié)合curl實(shí)現(xiàn)多線程抓取
PHP利用 Curl可以完成各種傳送文件操作,比如模擬瀏覽器發(fā)送GET,POST請求等等,然而因?yàn)閜hp語言本身不支持多線程,所以開發(fā)爬蟲程序效率并不高,因此經(jīng)常需要借助Curl Multi Functions 這個功能實(shí)現(xiàn)并發(fā)多線程的訪問多個url地址以實(shí)現(xiàn)并發(fā)多線程抓取網(wǎng)頁或者下載文件2015-07-07

