PHP Open Gzipped Xml

PHP open gzipped XML

As you didn't specify a PHP version, I am going to assume you are using PHP5.

I am wondering why people haven't suggested using the built in PHP compression streams API.

$linkToXmlFile = "compress.zlib:///path/to/xml/file.gz";
$xml = new XMLReader();
$xml->open($linkToXmlFile);

From what I understand, under the covers, it will transparently decompress the file for you and allow you to read it as if were a plain xml file. Now, that may be a gross understatement.

Problems getting gzipped XML files with curl PHP

Not sure why, but none of the other answers worked for me in the end. zlib was installed on the server, but the gzdecode() function was not defined in the library, and the gzuncompress gave me errors, as did compress.zlib://. They might work for you so, give them a try as well.

If you need to check if zlib is installed this stackoverflow answer or this answer can help. They provide this script:

<?php

echo phpversion().", ";

if (function_exists("gzdecode")) {
echo "gzdecode OK, ";
} else {
echo "gzdecode no OK, ";
}

if (extension_loaded('zlib')) {
echo "zlib extension loaded ";
} else {
echo "zlib extension not loaded ";
}

?>

This site gives another script that shows what zlib function are installed:

var_dump(get_extension_funcs('zlib'));

SOLUTION!!! These 2 functions did the trick for me. Just curl or use file_get_contents to grab the xml file, then use this script:

$xmlcontent = gzinflate(substr($xmlcontent,10,-8));

OR use this script to grab the xml file and get the contents (see more here):

$zd = gzopen($filename,"r");
$contents = gzread($zd,$fileSize);
gzclose($zd);

Thanks to all who helped me get this answer. Hope this helps someone else!

Load gzipped XML file with simplexml_load_file()

Read it to a string using file_get_contents()
decompress it using gzuncompress()
and load string as XML using simplexml_load_string().

reading xml from gz file

You do not need to save the file, you can pass it to gzdecode($string) and it will return uncompressed string:

$compressed = curl_exec($ch);
curl_close($ch);

$uncompressed = gzdecode($compressed);
// now you can use string as xml
$xml = simplexml_load_string($uncompressed);

Open remote gzip XML file for use with SimpleXML

Tried this ?

$xml = new SimpleXMLElement("compress.zlib://$url", NULL, TRUE);

PHP: How to uncompress GZ to a string?

You should be able to do it with the gzdecode function from the Zlib library.

$uncompressedXML = gzdecode(file_get_contents($url));

More about gzdecode() from the PHP Docs.

However, there is even an easier way, and it's by using a compression wrapper.

$uncompressedXML= file_get_contents("compress.zlib://{$url}");

Or even better:

$xmlObject=simplexml_load_file("compress.zlib://{$url}");

Don't forget to install and enable Zlib on your development/production server.

PHP gzread, gzfile, gzopen, etc.. all strip tags off of XML and return only the values

You're putting the XML in an HTML web page, so the browser is interpreting the XML tags as HTML tags.

Use htmlentities() to encode them so they'll be rendered literally.

    foreach ($lines as $line) {
echo htmlentities($line);
$contents2 = $contents2.$line;
}

You might want to show this in a <pre> block so the newlines and indentation will be preserved.



Related Topics



Leave a reply



Submit