最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入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ù)的使用示例:
復(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ò)誤。

相關(guān)文章

最新評論

梁平县| 阜城县| 施甸县| 小金县| 六枝特区| 普定县| 都安| 固阳县| 无为县| 安阳市| 西宁市| 武义县| 修武县| 邵东县| 马关县| 章丘市| 威远县| 万源市| 辰溪县| 通城县| 商河县| 肥乡县| 贵南县| 信阳市| 奉化市| 延寿县| 东乡| 丰宁| 隆子县| 柳江县| 尚志市| 弥勒县| 满洲里市| 新巴尔虎右旗| 陈巴尔虎旗| 工布江达县| 泸西县| 嵊州市| 德阳市| 郴州市| 南宫市|