PHP Classes

File: geofunc.php

Recommend this page to a friend!
  Classes of Luis Toscano   PHP Get Meta Tags from URL   geofunc.php   Download  
File: geofunc.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Get Meta Tags from URL
Extract meta tags of a Web page with a given URL
Author: By
Last change:
Date: 2 years ago
Size: 1,658 bytes
 

Contents

Class file image Download
<?php

// function to geocode address, it will return false if unable to geocode address
function geocode($address, $key){
 
   
// url encode the address
   
$address = urlencode($address);
    
   
// google map geocode api url
   
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key={$key}";
 
   
// get the json response
   
$resp_json = file_get_contents($url);
    
   
// decode the json
   
$resp = json_decode($resp_json, true);
 
   
// response status will be 'OK', if able to geocode given address
   
if($resp['status']=='OK'){
 
       
// get the important data
       
$lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
       
$longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
       
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
        
       
// verify if data is complete
       
if($lati && $longi && $formatted_address){
        
           
// put the data in the array
           
$data_arr = array();
            
           
array_push(
               
$data_arr,
                   
$lati,
                   
$longi,
                   
$formatted_address
               
);
            
            return
$data_arr;
            
        }else{
            return
false;
        }
        
    }
 
    else{
        echo
"<strong>ERROR: {$resp['status']}</strong>";
        return
false;
    }
}


$key = '';

print_r(geocode($address, $key));


?>