2011-08-29 26 views
1


我正在學習PHP,並試圖製作與外部網站有關係的應用程序。
我需要下載它。
所以我得到這個代碼:
如何使用PHP下載網站 - 我有一個小問題。 (或主要?)

$str = file_get_contents($url); 


應返回我的網站的HTML內容。
它適用於大多數網站,但對於特定的一個 - http://www.fxp.co.il - 它顯示廢話。
問題是什麼?我能做些什麼來解決它?
謝謝! enter image description here

+1

你爲什麼混淆網址? – PeeHaa

+1

內容正在發送gzip'd? –

+0

我出於隱私原因混淆了URL。咄。 - 谷歌的內容也gzipped - 並仍然表現出色... –

回答

2

那麼,你應該實際檢查響應頭,因爲他們告訴你有關返回的數據編碼file_get_contents

例如,如果它是gzip編碼的,則需要解壓縮它。

通常您不會注意到,因爲file_get_contents()以服務器知道它不支持壓縮的方式發送請求。

然而,一些服務器不在乎,反正你發送壓縮響應:

<?php 

$url = 'http://www.fxp.co.il/'; 

$buffer = file_get_contents($url); 

echo $url, '<hr>', '<pre>', implode("\n", $http_response_header), '</pre>'; 

$bare = gzdecode($buffer); 

echo '<hr>', htmlspecialchars(substr($bare, 0, 256)); 

輸出:

http://www.fxp.co.il/ 
------------------------------------------------------------ 
HTTP/1.1 200 OK 
Server: nginx/0.7.67 
Date: Mon, 29 Aug 2011 19:19:55 GMT 
Content-Type: text/html; charset=UTF-8 
Connection: close 
Set-Cookie: bb_lastvisit=1314607056; expires=Tue, 28-Aug-2012 19:12:44 GMT; path=/ 
Set-Cookie: bb_lastactivity=0; expires=Tue, 28-Aug-2012 19:12:44 GMT; path=/ 
X-Accel-Expires: 600 
Cache-control: must-revalidate, post-check=0, pre-check=0 
Pragma: cache 
Vary: Accept-Encoding,User-Agent 
Content-Encoding: gzip 
Content-Length: 14170 
Expires: Tue, 24 Jan 1984 08:00:00 GMT 
X-Header: Boost Citrus 1.9 
Cache-Control: must-revalidate, post-check=0, pre-check=0 
------------------------------------------------------------ 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="rtl" lang="he"> <head> <meta http-equiv="Content-Type" content="text/html; charset 

保重!

+0

它工作了一段時間,現在它只是停止在gzdecode工作...... –

+0

這就是奇怪的 - 這是因爲我沒有在我的代碼中的函數本身。我複製粘貼它從php.net ...現在它的作品。 PHP's wierd ... –

+0

嗯,我建議你在調試時啓用錯誤報告:'ini_set('display_errors',1);使用error_reporting(〜0);'。 – hakre

相關問題