file_get_contents函数抓取内容失败分析

用file_get_contents来抓取页面内容不成功,可能是因为有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。

下面是file_get_contents和curl两个函数同样功能的不同写法

file_get_contents函数的使用示例:

  1. < ?php
  2. $file_contents = file_get_contents(‘http://www.ccvita.com/’);
  3. echo $file_contents;
  4. ?>

  5. 换成curl函数的使用示例:

  6. < ?php
  7. $ch = curl_init();
  8. $timeout = 5;
  9. curl_setopt ($ch, CURLOPT_URL, ‘http://www.ccvita.com’);
  10. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  12. $file_contents = curl_exec($ch);
  13. curl_close($ch);

  14. echo $file_contents;
  15. ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值