2015-10-15 101 views
1

我想用幾種方法嘗試,然後想在這裏問這個問題..沒有辦法成功與我......我試圖解碼並從一個網站讀取數據使用gzip 我使用curl & php。當我嘗試解碼和打印結果,我得到的亂碼特殊字符,如一個長長的清單:無法使用curl解碼gzip php

JHWkdsU01EUXdWa1pXYTFOdFZsZFRiaz 
VoVW14S2NGbFljRmRXYkdSWVpFZEdWRT 
FYVWtoWmEyaExXVlpLTm1KR1VsWmlXR2 

如果我運行下面的PHP腳本直接我有錯誤,如

PHP Warning: gzdecode(): data error in /var/www/mn.php on line 20 

這裏是我當前的代碼:

<? 
$data_string = '9999'; 
$ch = curl_init('http://example.com/getN.php&keyword='); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Darwin/15.0.0'); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch,CURLOPT_ENCODING , 'gzip'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_TIMEOUT,5); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded', 
    'Accept-Encoding: gzip, deflate', 
    'Content-Length: ' . strlen($data_string)) 
); 


$result = gzdecode (curl_exec($ch)); 

curl_close($ch); 
print_r($result); 


?> 

我也試圖通過

a2enmod deflate 
    /etc/init.d/apache2 restart 
使放氣模塊

,使從php.ini中的zlib的

任我嘗試測試它直接

curl -sH 'Accept-encoding: gzip' http://example.com/getN.php&keyword=9999 | gunzip - 

我得到了相同的結果 這裏從網站

HTTP/1.1 200 OK 
Server: nginx 
Date: Thu, 15 Oct 2015 00:41:54 GMT 
Content-Type: text/html; charset=utf-8 
Transfer-Encoding: chunked 
Vary: Accept-Encoding 
X-Powered-By: PHP/5.4.31 
X-Frame-Options: SAMEORIGIN 
Content-Encoding: gzip 

的信息,請幫助

+0

它很難調試這對你自example.com的實際數據似乎沒有被gzipped – chiliNUT

回答

2

我注意到你的代碼有

curl_setopt($ch,CURLOPT_ENCODING , 'gzip'); 

gzdecode()稍後調用。如果指示接受編碼內容,cURL將自動爲您處理解碼,而無需在curl_exec()之後手動執行解碼。如果你告訴cURL接受編碼傳輸,它的返回值已經被解碼。

也就是說,您嘗試下載的頁面實際上可能不是用gzip編碼,而是另一種方法。正如manual說,可以嘗試使用一個空字符串:

# Enable all supported encoding types. 
curl_setopt($ch, CURLOPT_ENCODING, ''); 

這使得所有支持的編碼類型。並且不要使用gzdecode()。結果應該已經解碼。

+0

我試過有/沒有 (gzip,gzdecode,Accept-Encoding:gzip/deflate)都給了我相同的結果 (長長的亂碼列表字符)或錯誤(PHP警告:gzdecode():行號中的數據錯誤) 我也複製gzip代碼,並嘗試手動解碼它或不工作... bwt ..我有這[鏈接](https://www.samltool.com/gzip.php)網站他們解碼我的gzip代碼在線沒有任何問題..工作! ,以便在我的代碼或服務器設置中確認我的問題。 (我的服務器不解碼)。 –

+0

您不必自己設置Content-Length。如果您使用cURL發送HTTP POST,它將爲您計算內容長度,並自動添加所需的Content-Length標頭。如果cURL壓縮內容,它可能比'strlen($ data_string)'小。此外,完全刪除CURLOPT_HTTPHEADER,並添加一個設置爲true的CURLOPT_POST選項。 –

0

感謝所有,,終於開始工作,我接受你的建議後,並刪除gzdecode和其他一些人,並保持頭..接受編碼爲gzip和這裏的最終代碼

<? 
$data_string = '9999'; 
$ch = curl_init('http://example.com/getN.php&keyword='); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Darwin/15.0.0'); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_TIMEOUT,5); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded', 
'Accept-Encoding: gzip', 
'Content-Length: ' . strlen($data_string)) 
); 


$result = curl_exec($ch); 

curl_close($ch); 
print $result; 


?>