关于file_get_contents返回为空或函数不可用的解决方案
发布时间:2020-12-13 06:26:17 所属栏目:PHP教程 来源:网络整理
导读:如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都
如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! curl函数的使用示例:<div class="codetitle"><a style="CURSOR: pointer" data="23688" class="copybut" id="copybut23688" onclick="doCopy('code23688')"> 代码如下:<div class="codebody" id="code23688">$ch = curl_init(); $timeout = 5; curl_setopt ($ch,CURLOPT_URL,‘//www.52php.cn'); 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函数来代替。 <FONT style="COLOR: #ff0000">PS1、如果你的主机服务商把curl也关闭了,那你还是换个主机商吧!2、allow_url_fopen设为off,并不代表你的主机不支持file_get_content函数。只是不能打开远程文件而已。function_exists(‘file_get_contents')返回的是true。所以网上流传的《file_get_contents函数不可用的解决方法》还是不能解决问题。 错误代码:<div class="codetitle"><a style="CURSOR: pointer" data="15402" class="copybut" id="copybut15402" onclick="doCopy('code15402')"> 代码如下:<div class="codebody" id="code15402">if (function_exists(‘file_get_contents')) { $file_contents = @file_get_contents($url); }else{ $ch = curl_init(); $timeout = 30; curl_setopt($ch,$url); curl_setopt($ch,1); curl_setopt($ch,$timeout); $file_contents = curl_exec($ch); curl_close($ch); } 应改为:<div class="codetitle"><a style="CURSOR: pointer" data="93376" class="copybut" id="copybut93376" onclick="doCopy('code93376')"> 代码如下:<div class="codebody" id="code93376">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,$timeout); $file_contents = curl_exec($ch); curl_close($ch); } 最终代码:<div class="codetitle"><a style="CURSOR: pointer" data="32897" class="copybut" id="copybut32897" onclick="doCopy('code32897')"> 代码如下:<div class="codebody" id="code32897">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,$timeout); $file_contents = curl_exec($ch); curl_close($ch); } return $file_contents; } 用法:echo file_get_content(‘//www.52php.cn'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |