file_get_contents函数不能使用的解决方法
2010-06-02 11:09
399 查看
有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>
换成curl函数的使用示例:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数
< ?php
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
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;
}
?>
其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。
转载 http://blog.csdn.net/jiedushi/archive/2008/08/01/2752366.aspx
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>
换成curl函数的使用示例:
< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数
< ?php
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
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;
}
?>
其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。
转载 http://blog.csdn.net/jiedushi/archive/2008/08/01/2752366.aspx
相关文章推荐
- file_get_contents函数不能使用的解决方法
- file_get_contents函数不能使用的解决方法
- file_get_contents函数不能使用的解决方法
- PHP中使用file_get_contents抓取网页中文乱码问题解决方法
- 关于BT5不能使用apt-get命令的解决方法
- 学习php需要了解的点4 - PHP中file_get_contents函数抓取https地址出错的解决方法
- 当apt-get intall出问题的时候,而使用“apt-get -f install”不能解决问题的时候,使用一下方法兴许能够解决
- PHP中使用file_get_contents抓取网页中文乱码问题解决方法
- PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
- PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
- Windows 2003不能使用include file功能的解决方法
- PHP中file_get_contents函数抓取https地址出错的解决方法
- win7中C#的winForm编程使用savefiledialog不能弹出保存窗体的解决方法
- win10 搜索不能使用解决方法
- xcode升级后ios9.0不能使用http协议的版本的解决方法介绍
- 解决新版Qt中不能使用opengl中gluXXX()函数的方法
- 使用get方法提交form表单时出现乱码解决方法
- dedecms搜索页面不能使用arclist等标签的解决方法
- 关于在win7环境下线程里调用SaveFileDialog不能显示的解决方法
- SecureCRT远程连接Linux下的sqlplus中退格键不能使用之解决方法