PHP gets data from online XML

Source: Internet
Author: User

Because the API to connect Baidu map, and then through the latitude to get location information, but the location of the information is embedded in the URL by the latitude and longitude, generated XML file, because it is online, at that time, it was thought to get the data inside without downloading, because the use of code download is available to download, But the download to the specific path always fails, first put the failed code, someone can see, may be able to wake up, or there is a better way.

1<?PHP2 Define($filePath, ' d:\\phpstudy\\www\\children\\gprs\\ ');3 $filename= ' http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=xml&key= 28bcdd84fae25699606ffad27f8da77b ';4 //Types of files5 Header(' Content-type:text/xml ');6 //Download the displayed name7 Header("Content-disposition:attachment; filename={$filePath}.map.xml ");8 //ReadFile ("$filename");9 $content=file_get_contents(Map.XML);Ten Var_dump($content);

At this time can not var_dump out the data inside, just when the connection is opened automatically downloaded to the browser default download place, I set the file path at the beginning of the completely useless, the file or downloaded to the browser default download folder.

Later on the Internet to see, is directly to get the data in the URL, do not download down, sure enough is more convenient ah.

1<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>2<?PHP3 $content=file_get_contents(' http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=json&key= 28bcdd84fae25699606ffad27f8da77b ');4 $json= Json_decode ($content,true);5 Var_dump($json);6 $address=$json[' Result '] [' Formatted_address '];7 Echo' <br><br><br><br> ';8 Echo $address;

The first row of meta if not add the words will be garbled, now according to the link to obtain the latitude and longitude location of the specific position.

If the array display is not intuitive, you can simply click to view the source code and then get the desired location information based on the key.

PHP gets data from online XML

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.