使用file_get_contents()获取sohu博客乱码问题

来源:互联网 发布:汇率换算js代码 编辑:程序博客网 时间:2024/05/17 03:28

问题:

最后打印出来的结果都是乱码,经测试,新浪博客不会出现此问题,只有搜狐博客有次问题

 

 

 

原因:

获取的头部当中有Content-Encoding: gzip说明内容是GZIP压缩的 解压后就能得到内容了
PHP内置的file_get_contents不支持GZIP 可以试试CURL 好象能处理GZIP

 

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

 


解决办法:

1.定义一位外国高人写的gzip解码函数gzdecode():

 

2.调用此函数将获得的数据解码

 

 

 

iehttpheaders,httpwatch,FF的LiveHTTPHeaders,,FF的firebug,FF的httpfox都可以看头部信息的,还有其它很多工具。

 

原创粉丝点击