我需要从远程服务器检索 GZ 压缩 XML 文件并通过 simplexml_load_string
解析它。有没有办法在没有 uncompressing the GZ to a file 的情况下做到这一点,然后通过 simplexml_load_file 读取该文件?我想跳过对我来说似乎不必要的步骤。
最佳答案
您应该能够使用 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。
关于PHP:如何将 GZ 解压缩为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188054/