Загрузите XML-файл gzipped с помощью simplexml_load_file ()

Я пытаюсь загрузить gzipped XML-файл с помощью функции simplexml_load_file () PHP, но я не знаю, как его декодировать, чтобы я мог использовать данные в нем.

Прочитайте его в строке с помощью file_get_contents() распакуйте ее с помощью gzuncompress() и загрузите строку как XML с помощью simplexml_load_string() .

PHP имеет поддержку встроенного сжатия zlib , просто префикс пути вашего файла с данными gzip'ed с помощью compress.zlib:// и вы закончили:

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

Работает как шарм.

 /*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; }