2015-12-09 22 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);