Jump to content

Receiving compressed data and uncompressing - xml, php, gzip, deflate


samjsharples

Recommended Posts

I am being sent an XML feed which needs to be downloaded in a 'compressed way'.

The example I have been given is this:

 

$url = 'http://myurl.com';
$headers[] = "Accept-Encoding: gzip,deflate";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$data = curl_exec($ch);

 

but the output I am getting is jargon like this with loads of black diamond question marks:

 

+T-}#v]KHB$&GY'߷s,.4֫'?'9;*9^>j~ǫ}z|hqJԳ".o2)~b​U~I|

 

How to a convert it back into XML?

Many thanks

Edited by samjsharples
Link to comment
Share on other sites

First, you need CURLOPT_RETURNTRANSFER. Then $data will be a string and you won't get output immediately. You might already have done this for all I know.

 

There's a lot of manual work you could do to support encoding types. Don't do that.

Instead use CURLOPT_ENCODING: cURL will not only set the Accept-Encoding header for you but automatically decode the content as well.

$url = 'http://myurl.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$data = curl_exec($ch);
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.