PHP open gzipped XML

不打扰是莪最后的温柔 提交于 2019-11-28 07:46:05

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.

Pascal MARTIN

Maybe the function gzdecode could help you : the manual says (quote) :

Decodes a gzip compressed string

So, you'd have to :

  • download the XML data
  • get it as a string
  • decompress it with gzdecode
  • work on it with XMLReader

That would depend on the right extension (zlib I guess) beeing installed on your server, though...

Mark: Expanding on Pascal's post, here is some example code that should work for you

$xmlfile = fopen($linkToXmlFile,'rb');
$compressedXml = fread($xmlfile, filesize($linkToXmlFile));
fclose($xmlfile);
$uncompressedXml = gzdecode($compressedXml); 

$xml = new XMLReader();
$xml->xml($uncompressedXml);

Expanding on Pascal's post, here is some example code that should work for you

$xmlfile = fopen($linkToXmlFile,'rb');
$compressedXml = fread($xmlfile, filesize($linkToXmlFile));
fclose($xmlfile);
$uncompressedXml = gzdecode($compressedXml); 

$xml = new XMLReader();
$xml->xml($uncompressedXml);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!