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

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

Hi requinix

 

Thanks for your answer. I'm still struggling to use this. How can I use the variable $date once this has been executed? If I use print_r I just get a long page load then a timeout message but the XML isn't even that big? Any clues?

 

Thanks

Sam

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.