$url = 'http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=gfPnxaaiPyNIhM7c3eZ1kmEL';
$xml = simplexml_load_file($url);echo $city = $xml->xpath('//currentCity')[0];
foreach($xml->xpath('//weather_data') as $r) {
$date = (array)$r->date;
$dayPictureUrl = (array)$r->dayPictureUrl;
}
var_dump($city, $date, $dayPictureUrl);北京object(SimpleXMLElement)#2 (0) {
}
array(4) {
[0]=>
string(30) "周五(今天, 实时:24℃)"
[1]=>
string(6) "周六"
[2]=>
string(6) "周日"
[3]=>
string(6) "周一"
}
array(4) {
[0]=>
string(57) "http://api.map.baidu.com/images/weather/day/leizhenyu.png"
[1]=>
string(51) "http://api.map.baidu.com/images/weather/day/yin.png"
[2]=>
string(52) "http://api.map.baidu.com/images/weather/day/qing.png"
[3]=>
string(52) "http://api.map.baidu.com/images/weather/day/qing.png"
}
$xml = simplexml_load_file($url);echo $city = $xml->xpath('//currentCity')[0];
foreach($xml->xpath('//weather_data') as $r) {
$date = (array)$r->date;
$dayPictureUrl = (array)$r->dayPictureUrl;
}
var_dump($city, $date, $dayPictureUrl);北京object(SimpleXMLElement)#2 (0) {
}
array(4) {
[0]=>
string(30) "周五(今天, 实时:24℃)"
[1]=>
string(6) "周六"
[2]=>
string(6) "周日"
[3]=>
string(6) "周一"
}
array(4) {
[0]=>
string(57) "http://api.map.baidu.com/images/weather/day/leizhenyu.png"
[1]=>
string(51) "http://api.map.baidu.com/images/weather/day/yin.png"
[2]=>
string(52) "http://api.map.baidu.com/images/weather/day/qing.png"
[3]=>
string(52) "http://api.map.baidu.com/images/weather/day/qing.png"
}
解决方案 »
- php pre_replace() 高亮显示文字
- 招聘有经验PHP开发人员
- 请问为啥ob_start一上来就用?
- 来几道PHP题?大家做吧
- 如何由gbk或gb2312转为unicode
- 关于大旗网基于框架的内容发布系统
- 在本地用phpmyadmin建好的mysql数据库怎么传到网上空间里
- 这个功能怎么实现???急!!!!!在线等
- 应该怎样配置php的运行环境,才能在浏览器上看到php编程的运行效果?
- 怎么通过html上传图片并用php保存至服务器?我想做一个上传头像的功能
- 为什么json解析之后跟原来的不一样
- 我想获取我上传的绝对路径,可是只获取了文件名,和模版文件!求帮助怎么获取我上传的绝对路径。
$city = $xml->xpath('//currentCity');
echo $city[0];
再说,错误信息也得贴全了吧,不然别人如何判断问题所在?想来是你的 php 版本太低了
$city = $xml->xpath('//currentCity')[0];
改为
$city = current($xml->xpath('//currentCity'));
$date = (array)$r->date;
print_r($date);
}
这样子写的话为什么只有一行数据北京
2014-04-25
Array ( [0] => 周五(今天, 实时:24℃) )
应该周六,周日都出来的啊