PHP Developer

Saturday, 22 November 2014

Get Latitude, Longitude and Zip Code (Post Code) of area where website is open or upload in PHP

Get Latitude, Longitude and Zip Code (Post Code) of area where website is open or upload in PHP


<?php
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );  //For online website use this line

/*  $geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp) ); */   //On Localhost Use this line

if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) {

     echo $lat = $geoplugin['geoplugin_latitude'];   //Show Latitude of present Location
     echo $long = $geoplugin['geoplugin_longitude'];
   //Show Latitude of present Location   
    }

    $zipurl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$long."&sensor=true";
   
    $value = json_decode(file_get_contents($zipurl));
   
    foreach ($value->results as $result) {
        foreach($result->address_components as $addressPart) {
            if((in_array('postal_code', $addressPart->types))) {
                $state = $addressPart->long_name;   //Show Zip Code of present Location
            }
        }
    }
    echo $present_zipcde = $state;  
?>


Note :-  Add this code anywhere in website where you want to get or show Latitude, Longitude and Post Code

No comments:

Post a Comment