教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php file_get_contents函数的使用问题

php file_get_contents函数的使用问题

发布时间:2015-08-23   编辑:jiaochengji.com
使用file_get_contents抓取sohu blog时,出现乱码问题,但是抓取新浪blog时没有问题。

使用file_get_contents抓取sohu blog时,出现乱码问题,但是抓取新浪blog时没有问题。

解决方法:
由于获取的页面头部有Content-Encoding: gzip,说明页面内容经过了GZIP压缩,需要先解压。然后才能正确的输出内容。
PHP file_get_contents函数,好像不支持GZIP压缩,此时大家可以试试CURL,是可以处理GZIP压缩过的页面的。

附头信息示例:
 

复制代码 代码示例:
   array(11) {
      [0]=>
      string(15) "HTTP/1.1 200 OK"
      [1]=>
      string(35) "Content-Type: text/html;charset=gbk"
      [2]=>
      string(17) "Connection: close"
      [3]=>
      string(13) "Server: nginx"
      [4]=>
      string(35) "Date: Fri, 29 Oct 2010 00:36:51 GMT"
      [5]=>
      string(26) "Vary: Host,Accept-Encoding"
      [6]=>
      string(14) "Pragma: Public"
      [7]=>
      string(26) "Cache-Control: max-age=300"
      [8]=>
      string(38) "Expires: Fri, 29 Oct 2010 00:41:53 GMT"
      [9]=>
      string(22) "Content-Encoding: gzip"
      [10]=>
      string(14) "FSS-Cache: HIT"
    }

您可能感兴趣的文章:
php file_get_contents函数抓取页面信息的代码
php读取远程文件的三种方法分享
PHP读写文件生成HTML的代码举例
php提示Warning: file_get_contents(): couldn’t resolve
PHP fopen/file_get_contents与curl性能比较
file_get_contents访问http取回响应头
file_get_contents不能获取带端口的网址
PHP file_get_contents超时的设置方法
php 获取远程网页内容简单函数
PHP Warning: file_get_contents failed to open stream解决办法

关键词: file_get_contents   
[关闭]
~ ~