: This article describes how to use php to obtain data from countries, provinces, cities, and surrounding areas based on geographical coordinates. For more information about PHP tutorials, see. Function: when the App obtains the geographical coordinates of a user, it can know the data of the user in that country, province, city, and surrounding area based on the coordinates.
Principle: based on the Baidu Geocoding API, you need to register a Baidu developer first, and then apply for a Baidu AK (key)
Geocoding. class. php
$ Ak, 'location' => implode (',', array ($ latitude, $ longpolling), 'pois '=> $ pois, 'output' => 'json'); // request Baidu api $ response = self: toCurl (self: API, $ param); $ result = array (); if ($ response) {$ result = json_decode ($ response, true);} return $ result ;} /*** use curl to call Baidu Geocoding API * @ param String $ url request address * @ param Array $ param request parameter * @ return JSON */privatestaticfunctiontoCurl ($ url, $ param = array ()) {$ Ch = curl_init (); if (substr ($ url, 0,5) = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false ); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true); // check whether the SSL encryption algorithm exists from the certificate} curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ param )); $ response = curl_exec ($ ch); if ($ Error = curl_error ($ ch) {returnfalse;} curl_close ($ ch); return $ response ;}}?>
Demo. php
'; Print_r ($ result); echo'
';?>
Return example:
Array ([status] => 0 [result] => Array ([location] => Array ([lng] => 113.32778195925 [lat] => 23.137201991056) [formatted_address] => No. 35, Xiheng Street, sports, Tianhe district, Guangzhou, Guangdong, China [business] => Tianhe city, sports center, shahe [addressComponent] => Array ([city] => Guangzhou [country] => China [direction] => nearby [distance] => 14 [district] => Tianhe district [province] => Guangdong [street] => sports West Heng street [street_number] => 35 [country_code] => 0) [poiRegions] => Array () [sematic_description] => 58 meters north of Hongyi building [cityCode] => 257 ))
Source code: Click to view
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above describes how to use php to obtain data from countries, provinces, cities, and surrounding areas based on geographical coordinates, including related content. I hope to help my friends who are interested in PHP tutorials.