Php's xml operations are very simple,

Source: Internet
Author: User
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"
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.