当前位置: 代码迷 >> PHP >> file_get_contents获取XML的奇怪有关问题
  详细解决方案

file_get_contents获取XML的奇怪有关问题

热度:34   发布时间:2016-04-28 22:09:26.0
file_get_contents获取XML的奇怪问题

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); // 这一句加不加结果都一样
$contents=file_get_contents($url);
获取一个XML文件,得到的结果是:

<?xml version="1.0" encoding="UTF-8"?><cities>
<city><cityid>2421</cityid><cityname>上海</cityname></city>
<city><cityid>2420</cityid><cityname>天津</cityname></city>
</cities>

而我用浏览器或其他工具取得的源码都是这样的:

<?xml version="1.0" encoding="UTF-8"?><cities>
<city><id>2421</id><name>涓婃捣</name></city>
<city><id>2420</id><name>澶╂触</name></city>
</cities>

为什么会这样呢? 一个是cityid,另一个是id,一个是cityname,另一个是name

------解决方案--------------------
你的url是什么。。可以贴出来吧。
------解决方案--------------------
<?xml version="1.0" encoding="UTF-8"?><cities>
<city><cityid>2421</cityid><cityname>上海</cityname></city>
<city><cityid>2420</cityid><cityname>天津</cityname></city>
<city><cityid>2419</cityid><cityname>北京</cityname></city>
<city><cityid>2418</cityid><cityname>重庆</cityname></city>
<city><cityid>372</cityid><cityname>深圳</cityname></city>
<city><cityid>2422</cityid><cityname>广州</cityname></city>
.....


------解决方案--------------------
浏览器或file_get_contents都是如下结果

<?xml version="1.0" encoding="UTF-8"?><cities>
<city><cityid>2421</cityid><cityname>上海</cityname></city>
<city><cityid>2420</cityid><cityname>天津</cityname></city>
<city><cityid>2419</cityid><cityname>北京</cityname></city>
<city><cityid>2418</cityid><cityname>重庆</cityname></city>
...........................

------解决方案--------------------
楼主浏览器编码问题