Page 1 of 1

Parsing gzipped xml feed help

Posted: Wed Dec 16, 2009 2:18 pm
by qbawler311
I'm trying to deflate a gzipped (.gz) xml feed, the host of the feed requires that I: "set the header accept-encoding in request with value : gzip or gzip,deflate" I have a working xml parser but I can't get passed the .gz compression?

Here is my code below:

Code: Select all

 
<?php
    header('Accept-Encoding: gzip,deflate');
    $zp = gzopen("http://xml.url.com/xmlfeed/feed?", "r");
    gzpassthru($zp);
?>
 
The response I get back from the host of the feed:

Code: Select all

 
<response>
  <error-message>you must add 'accept-encoding' as 'gzip,deflate' to the header of your request</error-message>
</response>
 

Re: Parsing gzipped xml feed help

Posted: Wed Dec 16, 2009 3:10 pm
by AbraCadaver
You're not going to be able to use gzopen() and the header you're sending is to the client browser not the URL. You can probably use several different ways using fopen() or file_get_contents() and stream_context_create(), but here is a totally untested thought:

Code: Select all

$options = array('compress' => true);
$response = http_get('http://xml.url.com/xmlfeed/feed?');
$message = http_parse_message($response);
$body = gzuncompress($message->body);
 
//or maybe
 
$options = array('compress' => true);
$response = http_get('compress.zlib://http://xml.url.com/xmlfeed/feed?');
$message = http_parse_message($response);
$body = $message->body;
 
//or maybe
 
$options = array('http'=>array('Accept-Encoding'=>"gzip,deflate"));
$context = stream_context_create($options);
$data = file_get_contents('compress.zlib://http://xml.url.com/xmlfeed/feed?', null, $context);