關(guān)于file_get_contents返回為空或函數(shù)不可用的解決方案
更新時(shí)間:2013年06月24日 10:27:38 作者:
本篇文章是對(duì)file_get_contents返回為空或函數(shù)不可用的解決方案進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
如果你使用file_get_contents獲取遠(yuǎn)程文件內(nèi)容返回為空或提示該函數(shù)不可用,也許本文能幫到你!
使用file_get_contents和fopen必須空間開(kāi)啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時(shí)fopen和file_get_contents都不能打開(kāi)遠(yuǎn)程文件。如果你使用的是虛擬主機(jī)可以考慮用curl函數(shù)來(lái)代替。
curl函數(shù)的使用示例:
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘http://www.dbjr.com.cn');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
利用function_exists函數(shù)來(lái)判斷php是否支持file_get_contents,否則用curl函數(shù)來(lái)代替。
PS
1、如果你的主機(jī)服務(wù)商把curl也關(guān)閉了,那你還是換個(gè)主機(jī)商吧!
2、allow_url_fopen設(shè)為off,并不代表你的主機(jī)不支持file_get_content函數(shù)。只是不能打開(kāi)遠(yuǎn)程文件而已。function_exists(‘file_get_contents')返回的是true。所以網(wǎng)上流傳的《file_get_contents函數(shù)不可用的解決方法》還是不能解決問(wèn)題。
錯(cuò)誤代碼:
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(‘http://www.dbjr.com.cn');
使用file_get_contents和fopen必須空間開(kāi)啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時(shí)fopen和file_get_contents都不能打開(kāi)遠(yuǎn)程文件。如果你使用的是虛擬主機(jī)可以考慮用curl函數(shù)來(lái)代替。
curl函數(shù)的使用示例:
復(fù)制代碼 代碼如下:
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘http://www.dbjr.com.cn');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
利用function_exists函數(shù)來(lái)判斷php是否支持file_get_contents,否則用curl函數(shù)來(lái)代替。
PS
1、如果你的主機(jī)服務(wù)商把curl也關(guān)閉了,那你還是換個(gè)主機(jī)商吧!
2、allow_url_fopen設(shè)為off,并不代表你的主機(jī)不支持file_get_content函數(shù)。只是不能打開(kāi)遠(yuǎn)程文件而已。function_exists(‘file_get_contents')返回的是true。所以網(wǎng)上流傳的《file_get_contents函數(shù)不可用的解決方法》還是不能解決問(wèn)題。
錯(cuò)誤代碼:
復(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.dbjr.com.cn');
您可能感興趣的文章:
- 探討file_get_contents與curl效率及穩(wěn)定性的分析
- 深入php函數(shù)file_get_contents超時(shí)處理的方法詳解
- 詳解PHP內(nèi)置訪問(wèn)資源的超時(shí)時(shí)間 time_out file_get_contents read_file
- file_get_contents獲取不到網(wǎng)頁(yè)內(nèi)容的解決方法
- 比f(wàn)ile_get_contents穩(wěn)定的curl_get_contents分享
- PHP-CGI進(jìn)程CPU 100% 與 file_get_contents 函數(shù)的關(guān)系分析
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP下通過(guò)file_get_contents的代理使用方法
- php file_get_contents函數(shù)輕松采集html數(shù)據(jù)
- PHP file_get_contents 函數(shù)超時(shí)的幾種解決方法
相關(guān)文章
在PHP上顯示JFreechart畫(huà)的統(tǒng)計(jì)圖方法
在JSP上的servlet能完全的顯示出JFreechart畫(huà)的統(tǒng)計(jì)圖,但是和其他語(yǔ)言混合運(yùn)用就不能顯示了,下面為大家介紹下如何在PHP上顯示JFreechart2013-11-11php中根據(jù)變量的類(lèi)型 選擇echo或dump
有時(shí)候,我們想輸出一個(gè)變量,如果是數(shù)組的,則var_dump,如果是字符串之類(lèi)的,則echo即可2012-07-07PHP開(kāi)發(fā)中的錯(cuò)誤收集,不定期更新。
PHP開(kāi)發(fā)中的錯(cuò)誤收集,不定期更新。 php開(kāi)發(fā)的朋友需要用得到。2011-02-02簡(jiǎn)單談?wù)刾hp浮點(diǎn)數(shù)精確運(yùn)算
如果用php的+-*/計(jì)算浮點(diǎn)數(shù)的時(shí)候,可能會(huì)遇到一些計(jì)算結(jié)果錯(cuò)誤的問(wèn)題,所以基本上大部分語(yǔ)言都提供了精準(zhǔn)計(jì)算的類(lèi)庫(kù)或函數(shù)庫(kù),比如php有BC高精確度函數(shù)庫(kù),下面我們介紹一下一些常用的BC高精確度函數(shù)使用。2016-03-03PHP中文URL編解碼(urlencode()rawurlencode()
PHP中對(duì)于URL進(jìn)行編碼,可以使用 urlencode() 或者 rawurlencode(),二者的區(qū)別是前者把空格編碼為 '+',而后者把空格編碼為 '%20',不過(guò)應(yīng)該注意的是,在編碼時(shí)應(yīng)該只對(duì)部分URL編碼,否則URL中的冒號(hào)和反斜杠也會(huì)被轉(zhuǎn)義。2010-07-07PHP下通過(guò)系統(tǒng)信號(hào)量加鎖方式獲取遞增序列ID
前一陣子,設(shè)計(jì)LAJP時(shí)需要在PHP中生成唯一ID,看似小菜一碟卻著實(shí)讓我為難了,在Java中一個(gè)同步方法即可搞定的事,但在PHP中卻沒(méi)有好的解決思路。2009-09-09php 數(shù)組字符串搜索array_search技巧
本文給大家總結(jié)了一下PHP實(shí)現(xiàn)數(shù)組字符串搜索的幾種使用技巧,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-07-07php實(shí)現(xiàn)利用phpexcel導(dǎo)出數(shù)據(jù)
以下是對(duì)php中利用phpexcel導(dǎo)出數(shù)據(jù)的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08