0

I am getting some info from a https web server using PHP plus cURL. All info got as HTTP GET is ok but when I need to do some HTTP POST I get a no sense output. Web server is ok as If i get the info from a web browser all works ok.

I am using following code:

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); 

Answer header is:

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

Any idea about where the problem could be?

4
  • Could you give us the whole HTTP headers section? I mean, including the status header. Commented Nov 20, 2011 at 11:22
  • By nonsense output, you don't mean gzipped output do you? Can you explain what exactly your problem is? To be blunt: we're not magic and can't help you debug your code from afar without ample information. Also, have you tried to emulate a web browser request exactly? If the server is fine, then it's something in your request code that the server doesn't like. Commented Nov 20, 2011 at 11:35
  • Sorry for not to being more explicit. Request output is not a readable text as expected. I can only see symbols. Commented Nov 20, 2011 at 11:41
  • 2
    You say in your headers that you accept gzip, so the server responds with gzip'ed data. I don't see the problem. Commented Nov 20, 2011 at 11:44

1 Answer 1

3

It clearly shows its gziped...

Content-Encoding: gzip
Transfer-Encoding: chunked

Passing the returned data through the below function will inflate it back to readable content.

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Adding curl_setopt($ch,CURLOPT_ENCODING , "gzip"); to request works ok
Good, Im glad you got it sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.