一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

關于file_get_contents返回為空或函數不可用的解決方案

如果你使用file_get_contents獲取遠程文件內容返回為空或提示該函數不可用,也許本文能幫到你!
使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設置allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。如果你使用的是虛擬主機可以考慮用curl函數來代替。
curl函數的使用示例:
復制代碼 代碼如下:
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘http://www.jb51.NET');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

利用function_exists函數來判斷php是否支持file_get_contents,否則用curl函數來代替。
PS
1、如果你的主機服務商把curl也關閉了,那你還是換個主機商吧!
2、allow_url_fopen設為off,并不代表你的主機不支持file_get_content函數。只是不能打開遠程文件而已。function_exists(‘file_get_contents')返回的是true。所以網上流傳的《file_get_contents函數不可用的解決方法》還是不能解決問題。
錯誤代碼:
復制代碼 代碼如下:
if (function_exists(‘file_get_contents')) {
$file_contents = @file_get_contents($url);
}else{
$ch = curl_init();
$timeout = 30;
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);
}

應改為:
復制代碼 代碼如下:
if (function_exists(‘file_get_contents')) {//判斷是否支持file_get_contents
$file_contents = @file_get_contents($url);
}
if ($file_contents == ”) {//判斷$file_contents是否為空
$ch = curl_init();
$timeout = 30;
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);
}

最終代碼:
復制代碼 代碼如下:
function file_get_content($url) {
if (function_exists(‘file_get_contents')) {
$file_contents = @file_get_contents($url);
}
if ($file_contents == ”) {
$ch = curl_init();
$timeout = 30;
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;
}

用法:
echo file_get_content(‘http://www.jb51.NET');

php技術關于file_get_contents返回為空或函數不可用的解決方案,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 四虎欧美永久在线精品免费 | 2021最新久久久视精品爱 | www天天操 | 美女视频网站黄色 | 色黄网址| 国产综合自拍 | 1000部精品久久久久久久久 | 最新日本免费一区二区三区中文 | 激情图片激情小说 | 手机日韩理论片在线播放 | 激情福利视频 | 婷婷影音 | 精品视频在线播放 | 亚洲第一免费网站 | 国产精品人成 | 一区二区视频免费看 | 天天拍拍天天爽免费视频 | 四虎必出精品亚洲高清 | 黄免费网站 | 久久青青草原热精品 | 亚洲一区中文字幕 | 国产精品深夜福利免费观看 | 激情一区二区三区 | 精品伊人久久大线蕉色首页 | 国产伦视频 | xxx 日本韩国| 五月婷婷 六月丁香 | 欧美成人网在线综合视频 | 女人张腿让男桶免费视频网站 | 一区二区三区网站在线免费线观看 | 99国产成人高清在线视频 | 国内精品一区二区 | 黄篇网站在线观看 | 国产中文字幕在线免费观看 | 国产女人伦码一区二区三区不卡 | 美女被日视频 | 91精品久久久久久久99蜜桃 | 欧美三级成人 | 国产永久在线观看 | 国产一级二级在线观看 | 黄色小视频在线免费看 |