file_get_contents返回為空或函數(shù)不可用的解決方案-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“file_get_contents返回為空或函數(shù)不可用的解決方案”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)從2013年開始,先為安陸等服務(wù)建站,安陸等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為安陸企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

如果你使用file_get_contents獲取遠程文件內(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都不能打開遠程文件。如果你使用的是虛擬主機可以考慮用curl函數(shù)來代替。
curl函數(shù)的使用示例:


復制代碼 代碼如下:


$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘https://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ù)。只是不能打開遠程文件而已。function_exists(‘file_get_contents')返回的是true。所以網(wǎng)上流傳的《file_get_contents函數(shù)不可用的解決方法》還是不能解決問題。
錯誤代碼:


復制代碼 代碼如下:


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)改為:


復制代碼 代碼如下:


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(‘https://www.jb51.net');

“file_get_contents返回為空或函數(shù)不可用的解決方案”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

本文標題:file_get_contents返回為空或函數(shù)不可用的解決方案-創(chuàng)新互聯(lián)
標題路徑:http://muchs.cn/article36/hihpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)定制網(wǎng)站、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司