2015-12-09 21 views

回答

2

您应该可以使用Zlib库中的gzdecode函数来执行此操作。

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

更多关于gzdecode() from the PHP Docs

但是,还有一种更简单的方法,它是通过使用compression wrapper

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

甚至更​​好:

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

不要忘了安装和您的开发/生产服务器上启用的Zlib。

0

您可以使用gzuncompress()解压缩字符串,但是使用gzuncompress会有一些缺陷,如字符串长度的限制和校验和的一些问题。

您可以使用此简写代码实现您期望的结果,但我建议根据数据(您正在接收的)如何压缩来检查额外的资源。

<?php 
$compressed = gzcompress('<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Dont forget me this weekend!</body></note>', 9); 
$uncompressed = gzuncompress($compressed); 
$xml = simplexml_load_string($uncompressed); 
var_dump($xml); 

http://php.net/manual/en/function.gzcompress.php

http://php.net/manual/en/function.gzdecode.php

http://php.net/manual/tr/function.gzuncompress.php

Why PHP's gzuncompress() function can go wrong?

0

或者干脆:

$sitemap = 'http://example.com/sitemaps/sitemap.xml.gz'; 
$xml = new SimpleXMLElement("compress.zlib://$sitemap", NULL, TRUE);