Php is very easy to operate on xml, so I am in a hurry. api. map. baidu. comtelematicsv3weather? Location Beijing & amp; outputxml & amp; akgfPnxaaiPyNIhM7c3eZ1kmEL: how to assign the currentCity node value to $ city, and perform the XML operations in a loop by searching weather_data php.
Http://api.map.baidu.com/telematics/v3/weather? Location = Beijing & output = xml & ak = gfPnxaaiPyNIhM7c3eZ1kmEL
In this example, how to assign the currentCity node value to $ city to retrieve all nodes in weather_data cyclically and assign the value to the array? -"$ Date, To $ dayPictureUrl.
------ Solution --------------------
$ Url = 'http: // api.map.baidu.com/telematics/v3/weather? Location = Beijing & 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 );
Beijing object (SimpleXMLElement) #2 (0 ){
}
Array (4 ){
[0] =>
String (30) "Friday (today, real-time: 24 ℃ )"
[1] =>
String (6) "Saturday"
[2] =>
String (6) "Sunday"
[3] =>
String (6) "Monday"
}
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"
}