Maison > Article > développement back-end > php对xml的操作非常简单,急
http://api.map.baidu.com/telematics/v3/weather?location=北京&output=xml&ak=gfPnxaaiPyNIhM7c3eZ1kmEL
大神在哪里啊
$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"}
$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"}Parse error: syntax error, unexpected '[', expecting ',' or ';'
我的代码都是经实测验证的!
不然也贴不出结果来
我的代码都是经实测验证的!
不然也贴不出结果来
我的代码都是经实测验证的!
不然也贴不出结果来
那是你的问题呀
再说,错误信息也得贴全了吧,不然别人如何判断问题所在?
想来是你的 php 版本太低了
$city = $xml->xpath('//currentCity')[0];
改为
$city = current($xml->xpath('//currentCity'));
那是你的问题呀
再说,错误信息也得贴全了吧,不然别人如何判断问题所在?
想来是你的 php 版本太低了
$city = $xml->xpath('//currentCity')[0];
改为
$city = current($xml->xpath('//currentCity'));
foreach($xml->xpath('//weather_data') as $r) {$date = (array)$r->date;print_r($date);}