PHP cURL POST输出错误

时间:2011-11-20 11:17:52

标签: php curl

我从使用PHP + cURL的https Web服务器获取一些信息。所有信息都得到了,因为HTTP GET是可以的,但是当我需要做一些HTTP POST时,我得到一个无意义的输出。 Web服务器没问题如果我从Web浏览器获取信息一切正常。

我正在使用以下代码:

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

有关问题出在何处的任何想法?

1 个答案:

答案 0 :(得分: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;
}