Google trends Public Holidays Coupon Code Code Compiler

Get Zip Code From Address Using Google Maps API In PHP.

Dec 25, 2018

get zip code from address using Google Maps API in PHP. Here we’ll combine all code into a PHP and you only need to use this to find zipcode from an address.
Step 1:
GetZipCode(   function accept one parameter  ($address)  into the $address you need to provide the full address from where you want to get latitude and longitude. This function returns the latitude and longitude of address as an array if found, otherwise, return FALSE.
Step 2:
After that we use latitude and longitude for the zipcode. This google map API is accept one parameter  (latlng)   into the $lat $lng with comma separated, you need to provide latitude and longitude from where you want to get zip code. This API returns the ZIPCODE of latitude and longitude as an array if found, otherwise, return FALSE.

$address = '1600 Pennsylvania Ave NW, Washington, DC 20500, USA';
function GetZipCode($address){   

     //set address      
  $Address= str_replace(' ','+',$address);    
    //Send request and receive json data by address   
      $getGeocodeAddr = file_get_contents(''.$Address.'&sensor=false$key=GOOGLEAPIKEY'); 
        $outputData = json_decode($geocodeFromAddr);      
   //Get latitude and longitute from json data       
  $lat  = $outputData->results[0]->geometry->location->lat; 
       //Send request for zipcode data using latitude longitute   
      $getZip = file_get_contents(''.$lat.','.$long.'&sensor=true_or_false');
         $outputZip = json_decode($getZip);
           $addressData = $outputZip->results[0]->address_components;
            foreach($addressData as $addr){
                 if($addr->types[0] == 'postal_code'){
                     //Return the zipcode   
                  return $addr->long_name; 
    return false;  
             return false;
         return false;

Use GetZipCode() Function Like The Following.

$ZipData = GetZipCode($address);
$zipcode = $zipData?$zipData:'Not found';

Copyright 2024. All rights are reserved