php 使用file_get_contents的问题

没有做过网页抓取类似的功能,file_get_contents这个方法使用的次数也不多。

昨天在phpchina上看见一个网页询问说,使用file_get_contents在抓取souhu blog的时候,出现乱码问题,但是抓取新浪blog的时候却没有

乱码问题。这个问题,我不知道的怎么解决,更不知道为什么。今天,在phpchina上看到lz已经结贴了。答案如下:

获取的头部当中有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. }
复制代码
再次多谢lz提的问题,已经这位仁兄的答案。让我又学到了东西。
phpchina中帖子的连接为:http://bbs.phpchina.com/thread-199524-1-1.html
看样子,phpchina csdn 这样的论坛还是要多多看看的。要学的东西还是很多的。
原文地址:https://www.cnblogs.com/ainiaa/p/1867055.html