2

I have an API that returns results after being sent POST data.

Currently I am using fopen.

I am told that when sent the correct header, the API will return gzip compressed results.

How do I send the Accept Encoding header with fopen?

Currently, the relevant code looks like:

  $params = array('http' => array( 
     'method' => 'POST',
     'content' =>  $xml,
     'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
     'ignore_errors' => $ignoreErrors
  )); 

  $ctx = stream_context_create($params); 
  $fp = fopen($url, 'rb', false, $ctx);

All the examples / etc that are easily found in Google relate to people trying to read streams that are always gzip compressed or people looking to set the header so they can send compressed data.

Darren S
  • 83
  • 1
  • 9
  • why not to use CURL? `curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');` – Robert Jun 10 '13 at 12:17
  • @Darren - at www.php.net/stream_context_create - you have an example how to send multiple headers. It takes about 5 seconds to find that info, and it's **not** fopen, it's stream_context_create you need. – N.B. Jun 10 '13 at 12:18
  • @Robert - I tried CURL. Checking curl_getinfo it does not seem to be sending compressed data - 'content_type' => string 'text/html; charset=iso-8859-1'. – Darren S Jun 10 '13 at 13:02

3 Answers3

3

I could not get it to work at all with fopen/stream_get_contents.

As per one of the other suggestions, I tried CURL.

For some reason none of the following worked:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

I also tried some of the other suggestions given.

The following did work:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
Darren S
  • 83
  • 1
  • 9
  • i found that curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip')); solved my problem as well. thank you! – sd1sd1 Aug 13 '16 at 19:54
1

Try to add an empty Accept-Encoding header:

$params = array('http' => array( 
  'method' => 'POST',
  'content' =>  $xml,
  'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n"
              .'Accept-Encoding:' . "\r\n"
  'ignore_errors' => $ignoreErrors
)); 

The empty value is the same as identity what means not encoding should happen. Refer to the rfc that I've linked.

But note that you cannot be sure that the remote server respects this.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The reason I asked the question is because the provider of the API says that the Accept-Encoding header will be honored but it does not seem to be. Even after updating the code to say "Accept-Encoding: gzip, deflate", I still seem to be receiving data normally. – Darren S Jun 10 '13 at 12:37
  • `Accept-Encoding:identity` !! *Read* my answer :) – hek2mgl Jun 10 '13 at 12:40
  • The reason I said "EVEN after updating the code" is because I tried your method and it did not work either, so I tried something else and was letting you know neither worked. – Darren S Jun 10 '13 at 12:49
0

Add "Accept-Encoding:gzip,deflate" to the header param. And you have to concatenate optional headers there in order to send them all, currently you are only replacing them

Jura Khrapunov
  • 1,024
  • 6
  • 14
  • $optional_headers is null, it relates to something else I did not remove for the post. Adding Accept-Encoding to the header did not do anything, I tried that before posting. – Darren S Jun 10 '13 at 12:27
  • I also tried - http://stackoverflow.com/questions/3800147/how-do-i-use-file-get-contents-to-get-a-gziped-page-on-a-remote-web-server-in-p – Darren S Jun 10 '13 at 12:35
  • @DarrenS - I would try the following: check the API consuming with cURL extension (setting correct encoding header, of course) and see whether it works that way. If it wouldn't work either I would assume something wrong on the APIs side. ANd there are much more options to debug curl: http://stackoverflow.com/questions/3757071/php-debugging-curl – Jura Khrapunov Jun 10 '13 at 13:30