深入file_get_contents與curl函數(shù)的詳解
更新時(shí)間:2013年06月25日 18:03:42 作者:
本篇文章是對file_get_contents與curl函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
有些主機(jī)服務(wù)商把php的allow_url_fopen選項(xiàng)是關(guān)閉了,就是沒法直接使用file_get_contents來獲取遠(yuǎn)程web頁面的內(nèi)容。那就是可以使用另外一個(gè)函數(shù)curl。
下面是file_get_contents和curl兩個(gè)函數(shù)同樣功能的不同寫法
file_get_contents函數(shù)的使用示例:
< ?php
$file_contents = file_get_contents('http://m.fzitv.net');
echo $file_contents;
?>
換成curl函數(shù)的使用示例:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://m.fzitv.net');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
利用function_exists函數(shù)來判斷php是否支持一個(gè)函數(shù)可以輕松寫出下面函數(shù)
< ?php
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
?>
其實(shí)上面的這個(gè)函數(shù)還有待商榷,如果你的主機(jī)服務(wù)商把file_get_contents和curl都關(guān)閉了,上面的函數(shù)就會出現(xiàn)錯(cuò)誤。
下面是file_get_contents和curl兩個(gè)函數(shù)同樣功能的不同寫法
file_get_contents函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
< ?php
$file_contents = file_get_contents('http://m.fzitv.net');
echo $file_contents;
?>
換成curl函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://m.fzitv.net');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
利用function_exists函數(shù)來判斷php是否支持一個(gè)函數(shù)可以輕松寫出下面函數(shù)
復(fù)制代碼 代碼如下:
< ?php
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
?>
其實(shí)上面的這個(gè)函數(shù)還有待商榷,如果你的主機(jī)服務(wù)商把file_get_contents和curl都關(guān)閉了,上面的函數(shù)就會出現(xiàn)錯(cuò)誤。
您可能感興趣的文章:
- 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效率及穩(wěn)定性的分析
- 比file_get_contents穩(wěn)定的curl_get_contents分享
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP curl 或 file_get_contents 獲取需要授權(quán)頁面的方法
相關(guān)文章
PHP數(shù)據(jù)庫操作Helper類完整實(shí)例
這篇文章主要介紹了PHP數(shù)據(jù)庫操作Helper類,詳細(xì)分析了php操作mysql的連接、增刪改查及關(guān)閉連接等步驟,并給出了完整的mysql操作類Helper供大家參考,需要的朋友可以參考下2016-05-05
對squid中refresh_pattern的一些理解和建議
用于確定一個(gè)頁面進(jìn)入cache后,它在cache中停留的時(shí)間。refresh_pattern規(guī)則僅僅應(yīng)用到?jīng)]有明確過時(shí)期限的響應(yīng)。原始服務(wù)器能使用Expires頭部,或者Cache-Control:max-age指令來指定過時(shí)期限。2009-04-04
PHP has encountered an Access Violation 錯(cuò)誤的解決方法
一般是因?yàn)閑accelerator的問題,windows下容易出現(xiàn)這個(gè)問題。2010-01-01
php入門之連接mysql數(shù)據(jù)庫的一個(gè)類
php入門之連接mysql數(shù)據(jù)庫的一個(gè)類,學(xué)習(xí)php的朋友可以參考下2012-04-04
PHP實(shí)現(xiàn)限制IP訪問及提交次數(shù)的方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)限制IP訪問及提交次數(shù)的方法,涉及php針對客戶端來訪IP的獲取、判斷以及結(jié)合session記錄IP訪問次數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07

