Jump to content

[SOLVED] Printing raw compressed gzip response from cURL


Haberdasher

Recommended Posts

I'm having some trouble figuring out how to do this or if it's even possible. I'm executing a cURL request with PHP and using the CURLOPT_ENCODING option to tell the target server to send the response back with gzip compression, and I'm getting the 'Content-Encoding: gzip' header back in the cURL response. For testing purposes, I need to print the raw, compressed, gzip content in the browser that's being sent back. Anyone have any tips to point me in the right direction? I've found a lot of resources on how to gzip content and whatnot, but nothing on how to print the raw output. <sarcasm>I can't believe more people don't want to see raw gzip compression!</sarcasm>

 

Thanks!

-Dave

Link to comment
Share on other sites

Set the CURLOPT_RETURNTRANSFER parameter to 1 so the output of curl_exec() will be the response... then you can echo it to the browser and if you want to see the content printed in the browser, try setting the Content-Type of the response to the browser to "text/plain"... like this:

 

... your curl settings up here ...

$raw_gzip = curl_exec($ch);

header("Content-Type: text/plain");

echo $raw_gzip;

Link to comment
Share on other sites

No dice. I already have the CURLOPT_RETURNTRANSFER option set so that I can parse out the rest of the return. Adding the header you mentioned only printed out the return in plain text, it didn't print out the compressed content.

 

There has to be some way of preserving the compressed content before the browser is able to decompress it. Perhaps by catching it on the server before the page is generated and served. Or perhaps I'm missing some fundamental concept.

 

Here's the schema for what I'm doing:

 

web page form submits parameters to be used in cURL -> local server sends cURL to remote server, asks for response with gzip compression -> remote server executes request, sends back response with gzip compression -> local server receives response and serves webpage with parsed response > webpage displays

 

I'm think I should be able to capture the gzipped content at the 'local server receives response and serves webpage with parsed response' stage.

 

-Dave

Link to comment
Share on other sites

Well your server is receiving the compressed response using the curl functions, so if you are sending the output of the curl functions to the browser, then the curl functions must be decompressing the response... it is not the browser decompressing the response.  There may be a way to tell curl not to decompress the response...

Link to comment
Share on other sites

Of course! Thanks, that got me pointed in the right direction.

 

Instead of using

curl_setopt($ch, CURLOPT_ENCODING, "gzip")

 

I used:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));

 

The compressed content is then available in the normal return.

 

It would seem that if you use CURLOPT_ENCODING it not only sets the Accept-Encoding header but also tells cURL to decompress the return. I've verified that the output is the correct gzip content by comparing it to the output TCPMon returns from the same request.

 

Thanks!

-Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.