无法通过file_get_contents API解压缩数据

时间:2014-12-07 10:07:33

标签: php xml

我尝试从中获取数据的网址包含以下信息:

Remote Address:120.138.69.80:80
Request URL:http://mp3.zing.vn/xml/album-xml/ZGJHTlNFQzFNtLFJTDHZG
Request Method:GET
Status Code:200 OK (from cache)
Request Headers
Provisional headers are shown
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp;q=0.8
User-Agent:Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/39.0.2171.71 Safari/537.36
Response Headers
Cache-Control:maxage=1800
Content-Encoding:gzip
Content-Type:text/xml;charset=utf-8
Date:Sun, 07 Dec 2014 09:43:13 GMT
Expires:Sun, 07 Dec 2014 10:13:13 GMT
Pragma:public
Via:1.1 VNG-Cache-123:84 (Lusca)
X-Cache:MISS from VNG-Cache-123

所以我这样做了:

    $url = 'http://mp3.zing.vn/xml/album-xml/ZGJHTlNFQzFNtLFJTDHZG';
    $options = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'));
    $context = stream_context_create($options);
    $contents = file_get_contents($url, false, $context);
    echo $contents;

结果是很多奇怪的字符,如:

����ogvƯ���.��/`7p\����w�d�b�   M����dD��b/    �X4A�E�@>.�I7h���P�����'��w$��M2LSb.lQ�D����x��<��d�Y����?  ���������kI0����|�g���'w'˳�"8�߻�ӷ��[۲~��{w���O DZ�X��:��|

我假设返回的数据已被gzip压缩。所以我尝试使用gzdecode API如下:

    echo gzdecode($contents);

我得到了:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

非常感谢任何PHP专家可以帮助我进一步解决这个问题。

RC2

1 个答案:

答案 0 :(得分:1)

Content-Encoding是gzip使用Accept-Encoding: identity标头获取内容并使用header('Content-Type: text/xml')来回显数据,因为您获取的内容是XML

<?php

$ch = curl_init('http://mp3.zing.vn/xml/album-xml/ZGJHTlNFQzFNtLFJTDHZG');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//  use a header of Accept-Encoding: identity
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

header('Content-Type: text/xml');
echo $data = curl_exec($ch);