2011-11-20 101 views
0

我從使用PHP和cURL的https web服務器獲取一些信息。所有的信息都是HTTP GET,但是當我需要做一些HTTP POST時,我得到了一個沒有意義的輸出。 Web服務器是好的,因爲如果我從網絡瀏覽器獲取信息一切正常。PHP cURL POST錯誤輸出

我使用下面的代碼:

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"); 

if($method == "POST"){ 
    print_r($post_fields); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);  
    curl_setopt($ch,CURLOPT_HTTPHEADER, array (
     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
     "Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3", 
     "Accept-Encoding: gzip, deflate", 
     "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" 
    )); 
} 

if ($usecookie) { 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);  
} 

if ($refer != "") { 
    curl_setopt($ch, CURLOPT_REFERER, $refer); 
} 

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 

答頭:

HTTP/1.1 200 OK 
Date: Sun, 20 Nov 2011 11:04:39 GMT 
Server: Apache 
Cache-Control: must-revalidate 
Pragma: no-cache 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
max-age: Thu, 01 Jan 1970 00:00:00 GMT 
X-Powered-By: Servlet/2.4 JSP/2.0 
idWl: PRO-LOW16_6604 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Transfer-Encoding: chunked 
Content-Type: text/html; charset=UTF-8 

在哪裏這個問題可能是任何想法?

+0

你可以給我們整個HTTP頭部分嗎?我的意思是,包括狀態標題。 –

+1

結果是HTTP/1.1 200 OK – LooPer

+0

通過廢話輸出,你不是指gzipped輸出嗎?你能解釋一下你的問題究竟是什麼?直言不諱:我們不是魔術,並且無法幫助您在沒有充足信息的情況下遠程調試代碼。另外,你是否試圖模擬一個Web瀏覽器請求?如果服務器沒問題,那麼服務器不喜歡你的請求代碼。 – Corbin

回答

3

它清楚地表明其gziped ...

Content-Encoding: gzip 
Transfer-Encoding: chunked 

通過下面的傳遞函數返回的數據將膨脹回可讀的內容。

function gzdecoder($d){ 
    $f=ord(substr($d,3,1)); 
    $h=10;$e=0; 
    if($f&4){ 
     $e=unpack('v',substr($d,10,2)); 
     $e=$e[1];$h+=2+$e; 
    } 
    if($f&8){ 
     $h=strpos($d,chr(0),$h)+1; 
    } 
    if($f&16){ 
     $h=strpos($d,chr(0),$h)+1; 
    } 
    if($f&2){ 
     $h+=2; 
    } 
    $u = gzinflate(substr($d,$h)); 
    if($u===FALSE){ 
     $u=$d; 
    } 
    return $u; 
} 
+0

添加curl_setopt($ ch,CURLOPT_ENCODING,「gzip」);要求工作正常 – LooPer

+0

好,我很高興你把它分類。 –