中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

關(guān)于file_get_contents返回為空或函數(shù)不可用的解決方案

如果你使用file_get_contents獲取遠(yuǎn)程文件內(nèi)容返回為空或提示該函數(shù)不可用,也許本文能幫到你!
使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時fopen和file_get_contents都不能打開遠(yuǎn)程文件。如果你使用的是虛擬主機可以考慮用curl函數(shù)來代替。
curl函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
$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函數(shù)來判斷php是否支持file_get_contents,否則用curl函數(shù)來代替。
PS
1、如果你的主機服務(wù)商把curl也關(guān)閉了,那你還是換個主機商吧!
2、allow_url_fopen設(shè)為off,并不代表你的主機不支持file_get_content函數(shù)。只是不能打開遠(yuǎn)程文件而已。function_exists(‘file_get_contents')返回的是true。所以網(wǎng)上流傳的《file_get_contents函數(shù)不可用的解決方法》還是不能解決問題。
錯誤代碼:
復(fù)制代碼 代碼如下:
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);
}

應(yīng)改為:
復(fù)制代碼 代碼如下:
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);
}

最終代碼:
復(fù)制代碼 代碼如下:
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技術(shù)關(guān)于file_get_contents返回為空或函數(shù)不可用的解決方案,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 久久久久久国产 | 久久国产精品-国产精品 | 在线观看欧美一区 | 成人精品一区二区 | 欧美xxxx色视频在线观看免费 | 大香网伊人 | 日韩国产黄色片 | 中文字幕在线看第二 | 不卡一二三区 | 国产精品毛片无码 | 国产精品一区在线 | 中文字幕免费视频 | 婷婷开心激情综合五月天 | 欧美精品综合在线 | 精品熟人一区二区三区四区 | 国产精品美女 | 亚洲一二视频 | 国产精品欧美一区二区 | 中文字幕精品一区二区三区精品 | 日本一区二区不卡 | 亚洲精品一区二区二区 | 99精品一级欧美片免费播放 | 精品乱码一区二区三四区视频 | 一级网站 | 日本高清视频网站 | 久久99成人 | 久久久国产精品 | 中文字幕在线看人 | 国产精品久久久久久中文字 | 中文字幕一区二区三区四区五区 | 精品国产一二三区 | 久久久91| 狠狠久久综合 | 成人毛片视频免费 | 精品区| 激情av免费看 | 成人亚洲片 | 人成在线| 亚洲日本免费 | 精品国产乱码久久久久久闺蜜 | 国产精品明星裸体写真集 |