PHP:如何将 GZ 解压缩为字符串?

标签 php xml gzip

我需要从远程服务器检索 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/

相关文章:

PHP 将 GZ 文件解析为 XML

php - 从字符串中删除非 utf8 字符

linux - tar 存档 : How reliable is append?

php - 处理 php 形式的撇号时遇到问题

xml - 在scala中按属性对xml进行排序

python - OpenERP 中的完整性错误

java - jackson fastxml 将命名空间添加到根元素

node.js - 在调用 res.end() 之前,SSE 事件不会到达客户端

php - PHP 中的 iconv() 和 mb_convert_encoding() 有什么区别?

php - fopen() 返回错误 : failed to open stream: HTTP request failed! HTTP/1.1 404 未找到