php 使用file_get_contents的問題

來源:互聯網
上載者:User

沒有做過網頁抓取類似的功能,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 這樣的論壇還是要多多看看的。要學的東西還是很多的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.