深入file_get_contents與curl函數(shù)的詳解
更新時(shí)間:2013年06月25日 18:03:42 作者:
本篇文章是對(duì)file_get_contents與curl函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
有些主機(jī)服務(wù)商把php的allow_url_fopen選項(xiàng)是關(guān)閉了,就是沒(méi)法直接使用file_get_contents來(lái)獲取遠(yuǎn)程web頁(yè)面的內(nèi)容。那就是可以使用另外一個(gè)函數(shù)curl。
下面是file_get_contents和curl兩個(gè)函數(shù)同樣功能的不同寫法
file_get_contents函數(shù)的使用示例:
< ?php
$file_contents = file_get_contents('http://www.dbjr.com.cn');
echo $file_contents;
?>
換成curl函數(shù)的使用示例:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.dbjr.com.cn');
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ù)來(lái)判斷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ù)就會(huì)出現(xiàn)錯(cuò)誤。
下面是file_get_contents和curl兩個(gè)函數(shù)同樣功能的不同寫法
file_get_contents函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
< ?php
$file_contents = file_get_contents('http://www.dbjr.com.cn');
echo $file_contents;
?>
換成curl函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.dbjr.com.cn');
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ù)來(lái)判斷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ù)就會(huì)出現(xiàn)錯(cuò)誤。
您可能感興趣的文章:
- php基于curl重寫file_get_contents函數(shù)實(shí)例
- PHP CURL或file_get_contents獲取網(wǎng)頁(yè)標(biāo)題的代碼及兩者效率的穩(wěn)定性問(wèn)題
- php中file_get_contents與curl性能比較分析
- php采用file_get_contents代替使用curl實(shí)例
- 探討file_get_contents與curl效率及穩(wěn)定性的分析
- 比f(wàn)ile_get_contents穩(wěn)定的curl_get_contents分享
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP curl 或 file_get_contents 獲取需要授權(quán)頁(yè)面的方法
相關(guān)文章
PHP下利用header()函數(shù)設(shè)置瀏覽器緩存的代碼
PHP高級(jí)應(yīng)用學(xué)習(xí)筆記之 利用header()函數(shù)設(shè)置瀏覽器緩存2010-09-09PHP通過(guò)內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況
這篇文章主要介紹了PHP通過(guò)內(nèi)置函數(shù)memory_get_usage()獲取內(nèi)存使用情況,需要的朋友可以參考下2014-11-11PHP對(duì)表單提交特殊字符的過(guò)濾和處理方法匯總
本篇文章主要是對(duì)PHP對(duì)表單提交特殊字符的過(guò)濾和處理方法進(jìn)行了總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02PHP 文件編程綜合案例-文件上傳的實(shí)現(xiàn)
本篇文章是對(duì)PHP中文件上傳的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07PHP7基于curl實(shí)現(xiàn)的上傳圖片功能
這篇文章主要介紹了PHP7基于curl實(shí)現(xiàn)的上傳圖片功能,結(jié)合實(shí)例形式對(duì)比分析了php5.5之前與php7版本的curl圖片上傳功能相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-05-05