Google Geocode with PHP

google geocoding api
google map api php example code
geocoder php example
google geocoding api free
google maps php mysql demo
google maps api
how to geocode addresses in google maps
geocoder android

I'm trying to get latitude and longitude from Google's Geocode API. The $url I'm using outputs the correct JSON however something in my script isn't working correctly. It's not outputting the $lati & $longi variables. Please check see the script below:

PHP:

<?php
$address   = 'Tempe AZ';
$address   = urlencode($address);
$url       = "https://maps.google.com/maps/api/geocode/json?sensor=false&address={$address}";
$resp_json = file_get_contents($url);
$resp      = json_decode($resp_json, true);

    if ($resp['status'] == 'OK') {
        // get the important data
        $lati  = $resp['results'][0]['geometry']['location']['lat'];
        $longi = $resp['results'][0]['geometry']['location']['lng'];
        echo $lati;
        echo $longi;

    } else {
        return false;
    }

?>

I brought in the Guzzle http package at the top of my Laravel file/class: use GuzzleHttp\Client;

Then, to take an address and convert it to lat and lng to save with PHP/Laravel and Google Maps API, I added this code in my save() method:

    // get latitude and longitude of address with Guzzle http package and Google Maps geocode API
    $client = new Client(); // GuzzleHttp\Client
    $baseURL = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
    $addressURL = urlencode(request('street')) . ',' . urlencode(request('city')) . ',' . urlencode(request('state')) . '&key=' . env('GOOGLE_MAPS_API_KEY');
    $url = $baseURL . $addressURL;
    $request = $client->request('GET', $url);
    $response = $request->getBody()->getContents();
    $response = json_decode($response);
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;

Google Geocode with PHP, It turned out to be a proxy issue. This thread helped: Using proxy with file_get_contents. Added this: $opts = array( 'http' => array( 'method'  PHP Google Geocode an Address Example How to Google Geocode an Address with PHP I have a properly listing site I support that allows users to search for properties within a distance from a specific zip code.

It turned out to be a proxy issue. This thread helped: Using proxy with file_get_contents

Added this:

$opts = array(
    'http' => array(
        'method' => "GET",
        'header' => "Accept-language: en\r\n".
        "Cookie: foo=bar\r\n",
        'proxy' => 'tcp://proxy.proxy.com:8080',
    )
);
$context = stream_context_create($opts);

// open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);

Using MySQL and PHP with Google Maps, It suits people with intermediate knowledge of MySQL, PHP and XML. can use a batch geocoding service to convert addresses into latitudes and longitudes  Google Geocoding API with PHP 13 October 2011 If you want to know where a particular address is by latitude and longitude, but only have the normal address details like street, city, zip, country, etc. then the Google Geocoding API should be able to help.

Your script works for me too. Maybe you will get an error calling:

$resp_json = file_get_contents($url);.

If that happens you should do this:

$resp_json = @file_get_contents($url);
 if ($resp_json === FALSE) {
  ///logic for exception
} else {
  ///do your logic hear
}

Developer Guide | Geocoding API, composer require geocoder-php/google-maps-provider php-http/guzzle6-adapter​. Or using the curl client (you'll need to provide a PSR7 implementation such as  PHPZAG Team PHP Geocoding is the process of converting address into geographic coordinates to show the geo-coded data on the map. For example if you have a particular address and wants to locate that address on the map or wants to create a store locator, then Google Maps Geocoding API can help you to handle this.

geocoder-php/Geocoder: The most featured Geocoder , [READ ONLY] Provider for Google maps. Contribute to geocoder-php/google-​maps-provider development by creating an account on GitHub. Google Geocode with PHP. Ask Question Asked 4 years, 10 months ago. Active 1 year, 4 months ago. Viewed 2k times 0. I'm trying to get latitude and longitude from

geocoder-php/google-maps-provider: [READ ONLY , In this example we will explain Google Map Geocoding, what it means and how we can manipulate the Google Map Geocoding API. For this  php-geocode is a simple offline PHP geocoding library. It is useful when geocoding web services (e.g., Google Geocoding) are too limiting. These web services often impose a limit on the number of requests and request rates, making it too limiting for batch processes. php-geocode performs geocoding on your server, taking away that limitations.

PHP Google Geocode Example, Easily use the Google Geocoding API in PHP to get the latitude and longitude from an address. We've created a helper class for you quickly get  Google Map with a Marker with PHP & Google Map API. This php tutorials help to understand google map and integration of google map JavaScript api using PHP and jQuery. We will create map using constant address and get coordinate to display marker on that location.

Comments
  • Your script works for me, maybe the https-wrapper is not available in your environment( see: stackoverflow.com/questions/1975461/… )
  • Thanks. The wrapper was available but it ended up to be a proxy issue. Appreciate the help.