file_get_contents函数抓取内容失败分析
2012-05-03 13:08
239 查看
用file_get_contents来抓取页面内容不成功,可能是因为有些主机服务商把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;
?>
下面是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;
?>
相关文章推荐
- 深入file_get_contents函数抓取内容失败的原因分析
- php利用file_get_contents函数抓取网页指定内容
- 抓取网页内容,用file_get_contents函数
- PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意
- PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
- php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容
- 经典抓取网络数据方法效率分析(fsockopen/curl/file_get_contents)
- 用file_get_contents抓取远程网址内容
- 关于测试curl 与file_get_contents抓取内容效率及稳定性
- php使用file_get_contents函数抓取页面信息
- 关于php服务器代理上网的时候使用file_get_contents函数取不到内容的问题的解决方案
- 学习php需要了解的点4 - PHP中file_get_contents函数抓取https地址出错的解决方法
- PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意
- PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
- file_get_contents获取json内容处理失败的问题
- php中的file_get_contents函数------获取文件内容
- PHP中file_get_contents函数抓取https地址出错的解决方法
- PHP中使用file_get_contents抓取网页中文乱码问题解决方法
- php中file_get_contents与curl性能比较分析