2013-03-22 65 views
0

這不會獲得gzip內容,而是普通內容。如何讓file_get_contents使用https發送標題?帶有file_get_contents的https標頭

$url = 'https://www.google.co.in/'; 

///Try to fetch compressed content using the file_get_contents function 
$opts = array(
'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en-US,en;q=0.8\r\n" . 
       "Accept-Encoding: gzip,deflate,sdch\r\n" . 
       "Accept-Charset:UTF-8,*;q=0.5\r\n" 
) 
); 

$context = stream_context_create($opts); 
$zipped_content = file_get_contents($url ,false,$context); 

echo $zipped_content; 

print_r($http_response_header); 

如果URL http://www.yahoo.co.in然後用gzip壓縮的內容供應(並確認,它看起來像垃圾)。

但是當使用「https://」時,似乎file_get_contents不會發送指定的標頭。

+0

您可能需要使用'fread',而不是二進制安全的。 'file_get_contents'將數據加載爲一個字符串......並不是真正想要的。 – 2013-03-22 05:00:48

+0

檢查您的瀏覽器發送給Google的標題並嘗試將它們全部發送出去。 – sectus 2013-03-22 05:27:02

+0

@sectus標題都可以 – 2013-03-22 06:50:23

回答

1

頁眉沒有確定......添加用戶代理,它會被罰款。

"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4\r\n". 

爲什麼?谷歌決定。

+0

工作!確實谷歌決定 – 2013-03-22 08:01:35

0

試試這個

$url = "https://www.google.co.in/"; 
    $ch = curl_init();  
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip')); 
    $contents = curl_exec($ch); 
    curl_close($ch); 
+0

我需要gzip內容,curl代碼會將我不需要的頁面的最終html內容提取給我。 – 2013-03-22 06:49:37