Download the gzipped xml file with simplexml_load_file ()

I am trying to load a gzipped XML file using the simplexml_load_file () function of PHP, but I do not know how to decode it, so I can use the data in it.

+4
source share
3 answers

Read it in a line using file_get_contents() unzip it using gzuncompress() and load the line as XML using simplexml_load_string() .

+3
source

PHP has support for zlib compression , just prefixing the path of your gzip'ed data file with compress.zlib:// and you're done:

 $xml = simplexml_load_file("compress.zlib://test.xml"); 

It works like a charm.

+11
source
 /*EDIT print_r(gzdecoder(simplexml_load_file('test.xml'))); (Not sure without testing that the internals of what loads the xml in *_load_file would see the gzipped content as invalid) */ $xml=simplexml_load_string(gzdecoder(file_get_contents('test.xml'))); print_r( $xml); function gzdecoder($d){ $f=ord(substr($d,3,1)); $h=10;$e=0; if($f&4){ $e=unpack('v',substr($d,10,2)); $e=$e[1];$h+=2+$e; } if($f&8){ $h=strpos($d,chr(0),$h)+1; } if($f&16){ $h=strpos($d,chr(0),$h)+1; } if($f&2){ $h+=2; } $u = gzinflate(substr($d,$h)); if($u===FALSE){ $u=$d; } return $u; } 
+4
source

All Articles