Curl simulation launches Baidu Map API POST request

Source: Internet
Author: User

Note: Getting started is a GET request, relatively simple, and then a query to make a POST request, there are several places specifically explained:
One, $address, must be preached, $city do not preach;
Second, AK with the previous key has been, need to apply, I do not seem to apply for direct online search for the use;
Third, if you want to callback a function, $data inside the callback can fill in the name of the callback function you want to fill out, if you want the result of the direct output is NULL, (this is a pit, because there is no experience AH);
Four, the JSON object processing, Json_decode really powerful, but has just been printed with print_r, the result is returned with the parameters of the function, showing the parameters, with the var_dump to find the reason, a bit of pit,
Five, output: Control the export format, there are XML types, have not tried, read the details of the document go;
Json_decoded (String, True) if you want to compile an array, and the second parameter is false by default;
Code below, pro-test available;
<code php>
protected function _get_coordinate_by_name ($address, $city = ") {
$url = ' http://api.map.baidu.com/geocoder/v2/';

$data = Array (
' ak ' = ' e4805d16520de693a3fe707cdc962045 ',
' Callback ' = null,
' Output ' = ' json ',
' Address ' and $address,
' City ' and $city,
);

$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, curlopt_header,0);
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, Curlopt_postfields, $data);
curl_setopt ($ch, Curlopt_referer, ' http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding ');
curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/42.0.2311.90 safari/537.36 ');
curl_setopt ($ch, Curlopt_returntransfer, 1);
$ret = curl_exec ($ch);
Curl_close ($ch);
$ret = Json_decode ($ret, true);
$_coord[' lat '] = $ret [' result '] [' location '] [lat '];
$_coord[' lng ' = $ret [' result '] [' location '] [' LNG '];

return $_coord;
}
</code>
Note: http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding

Curl simulation launches Baidu Map API POST request

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.