国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

file_get_contents函數不能使用的解決方法

2019-11-17 04:23:28
字體:
來源:轉載
供稿:網友

有些主機服務商把php的allow_url_fopen選項是關閉了,就是沒法直接使用file_get_contents來獲取遠程web頁面的內容。那就是可以使用另外一個函數curl。

下面是file_get_contents和curl兩個函數同樣功能的不同寫法

file_get_contents函數的使用示例:

< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>

換成curl函數的使用示例:

< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com');
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函數來判斷php是否支持一個函數可以輕松寫出下面函數

< ?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;
}
?>
其實上面的這個函數還有待商榷,如果你的主機服務商把file_get_contents和curl都關閉了,上面的函數就會出現錯誤。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平远县| 和田县| 十堰市| 张北县| 淮安市| 黎川县| 大田县| 苍溪县| 礼泉县| 府谷县| 山阴县| 临猗县| 都安| 天柱县| 桃江县| 屯门区| 博野县| 修武县| 灵川县| 广灵县| 梁山县| 垫江县| 伊宁市| 任丘市| 上犹县| 公安县| 虞城县| 钦州市| 北宁市| 漾濞| 景谷| 黑水县| 通江县| 方城县| 门源| 特克斯县| 黄山市| 广饶县| 南通市| 靖宇县| 公主岭市|