Parsing gzipped xml feed help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qbawler311
Forum Newbie
Posts: 1
Joined: Wed Dec 16, 2009 2:09 pm

Parsing gzipped xml feed help

Post 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>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Parsing gzipped xml feed help

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply