Php's xml operations are very simple and urgent.
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.
Reply to discussion (solution)
Where is the great god?
$ 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 "}
$ 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 "}
Parse error: syntax error, unexpected '[', expecting ',' or ';'
My code is verified by actual tests!
Otherwise, no results will be posted.
My code is verified by actual tests!
Otherwise, no results will be posted.
Don't get angry. this is my mistake. it seems that there is a problem with the brackets.
My code is verified by actual tests!
Otherwise, no results will be posted.
OK. it seems that the data to be written separately cannot be directly echo $ city = $ xml-> xpath ('// currentCity') [0];
Yes.
$ City = $ xml-> xpath ('// currentcity ');
Echo $ city [0];
That's your problem.
Besides, the error information must be fixed. Otherwise, how can someone determine the problem?
Your php version is too low.
$ City = $ xml-> xpath ('// currentcity') [0];
Change
$ City = current ($ xml-> xpath ('// currentcity '));
That's your problem.
Besides, the error information must be fixed. Otherwise, how can someone determine the problem?
Your php version is too low.
$ City = $ xml-> xpath ('// currentcity') [0];
Change
$ City = current ($ xml-> xpath ('// currentcity '));
Cainiao doesn't. sorry, the moderator.
foreach($xml->xpath('//weather_data') as $r) {$date = (array)$r->date;print_r($date);}
Why is there only one row of data written in Beijing?
2014-04-25
Array ([0] => Friday (today, real-time: 24 ℃ ))
It should be Saturday and Sunday.