关于file_get_contents返回为空或函数不可用的解决方
网络编程 2021-07-05 09:49www.168986.cn编程入门
本篇文章是对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_set ($ch, CURLOPT_URL, ‘https://.jb51.');
curl_set ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set ($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
用法:
echo file_get_content(‘https://.jb51.');
使用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_set ($ch, CURLOPT_URL, ‘https://.jb51.');
curl_set ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set ($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($ch, CURLOPT_RETURNTRANSFER, 1);
curl_set($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
用法:
echo file_get_content(‘https://.jb51.');
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程