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