2011-11-20 108 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

好,我很高兴你把它分类。 –