PHP's operation of XML is very simple and urgent

Source: Internet
Author: User
Tags parse error
Http://api.map.baidu.com/telematics/v3/weather?location= Beijing &output=xml&ak=gfpnxaaipynihm7c3ez1kmel

This gets how XML assigns the value of the CurrentCity node to $city, loops through all the nodes under the Weather_data, assigns 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, Live: 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"/http Api.map.baidu.com/images/weather/day/yin.png "  [2]=>  string" http://api.map.baidu.com/images/ Weather/day/qing.png "  [3]=>  string" 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, Live: 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"/http Api.map.baidu.com/images/weather/day/yin.png "  [2]=>  string" http://api.map.baidu.com/images/ Weather/day/qing.png "  [3]=>  string" Http://api.map.baidu.com/images/weather/day/qing.png "}

Parse error:syntax error, unexpected ' [', expecting ', ' or '; '

My code is verified by the test!
Or you can't put the results.

My code is verified by the test!
Or you can't put the results.

Don't be angry, my side is this mistake ah, seems to be the parentheses have problems

My code is verified by the test!
Or you can't put the results.

OK, seemingly to separate write can not directly echo $city = $xml->xpath ('//currentcity ') [0];

Can do this
$city = $xml->xpath ('//currentcity ');
echo $city [0];

That's your problem.
Besides, the error message has to be affixed to the whole, otherwise how others judge the problem?

I think it's your PHP version that's too low.
$city = $xml->xpath ('//currentcity ') [0];
Switch
$city = current ($xml->xpath ('//currentcity '));

That's your problem.
Besides, the error message has to be affixed to the whole, otherwise how others judge the problem?

I think it's your PHP version that's too low.
$city = $xml->xpath ('//currentcity ') [0];
Switch
$city = current ($xml->xpath ('//currentcity '));

Rookie totally not Ah, also please moderator forgive me ah

foreach ($xml->xpath ('//weather_data ') as $r) {$date = (array) $r->date;print_r ($date);}

If it's written like this, why only one row of data Beijing
2014-04-25
Array ([0] = Friday (today, Real time: 24 ℃))
It's supposed to be Saturday and Sunday.
  • Related Article

    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.