Simplexml_load_string() fail to parse error

后端 未结 5 2011
眼角桃花
眼角桃花 2020-12-03 08:53

I\'m trying to load parse a Google Weather API response (Chinese response).

Here is the API call.

// This code fails with the following error
$xml =          


        
5条回答
  •  难免孤独
    2020-12-03 09:22

    Update: I can reproduce the problem. Also, Firefox is auto-sniffing the character set as "chinese simplified" when I output the raw XML feed. Either the Google feed is serving incorrect data (Chinese Simplified characters instead of UTF-8 ones), or it is serving different data when not fetched in a browser - the content-type header in Firefox clearly says utf-8.

    Converting the incoming feed from Chinese Simplified (GB18030, this is what Firefox gave me) into UTF-8 works:

     $incoming = file_get_contents('http://www.google.com/ig/api?weather=11791&hl=zh-CN');
     $xml = iconv("GB18030", "utf-8", $incoming);
     $xml = simplexml_load_string($xml);
    

    it doesn't explain nor fix the underlying problem yet, though. I don't have time to take a deep look into this right now, maybe somebody else does. To me, it looks like Google are in fact serving incorrect data (which would surprise me. I didn't know they made mistakes like us mortals. :P)

提交回复
热议问题