php获取在线xml的数据

  因为连接百度地图的API,然后通过经纬度得到位置信息,可是得到的位置信息是通过将经纬度嵌在url里面,生成xml文件后,因为是在线的,当时就想到在不下载的情况下获取里面的数据,因为使用代码下载是可以下载,但是下载到具体的路径总是失败,先放上失败的代码,有人看到的话,也许能提个醒,或者有更好的办法。

 1 <?php
 2 define($filePath,'D:\\phpStudy\\WWW\\children\\gprs\\');
 3 $filename = 'http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=xml&key=28bcdd84fae25699606ffad27f8da77b';
 4 //文件的类型
 5 header('Content-type: text/xml');
 6 //下载显示的名字
 7 header("Content-Disposition: attachment; filename={$filePath}.map.xml");
 8 //readfile("$filename");
 9 $content=file_get_contents(map.xml);
10 var_dump($content);

这时不能够var_dump出里面的数据,只是在打开该连接的时候就自动下载到浏览器默认下载的地方,我在开头设置的文件路径完全就用不了,文件还是下载到浏览器默认下载的文件夹里。

  后来在网上看到,就是直接获取该url中的数据,不用下载下来,果然是方便多了啊。

1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2 <?php
3 $content = file_get_contents('http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=json&key=28bcdd84fae25699606ffad27f8da77b');
4 $json = json_decode($content,true);
5 var_dump($json);
6 $address=$json['result']['formatted_address'];
7 echo '<br><br><br><br>';
8 echo $address;

第一行的meta如果不加上的话就会乱码,现在根据该链接就能获取到经纬度定位的具体位置了。

如果该数组显示不直观,可以直接点击查看源代码,然后根据key获取所需要的位置信息。

posted @ 2015-09-02 19:20  todaytoday  阅读(1326)  评论(0编辑  收藏  举报