First, encapsulate a method for getting header header files in the Function.php method file.
if (!function_exists (' getallheaders ')) {
function Getallheaders () {
$headers = Array ();
foreach ($_server as $name = = $value) {
if (substr ($name, 0, 5) = = ' Http_ ') {
$headers [Str_replace (', '-'), Ucwords (Strtolower (Str_replace (' _ ', ', ' substr ($name, 5))])] = $value;
}
}
return $headers;
}
}
Then, call the method in the corresponding controller to get the data, first check the session there is no location information.
if (!$_session[' mylocation ')
{
Get positional coordinates
$location = Getallheaders ();
$this->get_location ($location); Calling the encapsulated method
}
Finally, a method is encapsulated to request that the Baidu interface return specific location information and coexist in the session.
Public Function get_location ($location)
{
$lat = $location [' Latitude '];
$LNG = $location [' Longitude '];
if ($lat && $lng)
{
$url = ' http://api.map.baidu.com/geocoder/v2/?ak=5BFNbSgnVF5g2O72NpvTDxFm&location= '. $lat. ‘,‘ . $lng. ' &output=json&pois=1 ';
$html = Json_decode (file_get_contents ($url), true);
if ($html)
{
$myLocation = Array (
' City ' = $html [' result '] [' addresscomponent '] [' City '],
' Addr ' = Isset ($html [' Result '] [' addresscomponent '] [' Street ']? $html [' Result '] [' addresscomponent '] [' Street ']: $html [' result '] [' formatted_address '],
' LNG ' = $html [' result '] [' location '] [' LNG '],
' Lat ' = $html [' result '] [location '] [lat '],
);
$_session[' mylocation '] = $myLocation;
}
}
}
PHP gets the coordinates that iOS or Android pass through the header (header), obtains the specific city and address through the Baidu interface, and deposits it into the session.