Skip to content

Instantly share code, notes, and snippets.

@alex-georgiou
Created July 16, 2019 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-georgiou/aefb333ef1497b15315ee01d2f9e35a8 to your computer and use it in GitHub Desktop.
Save alex-georgiou/aefb333ef1497b15315ee01d2f9e35a8 to your computer and use it in GitHub Desktop.
Example query to the Google Places API, showcasing how to first get a place id from a text description, and then how to get details about that place
<?php
/**
* Example query to the Google Places API, showcasing how to first get a place id from a text description,
* and then how to get details about that place.
*
* @author Alexandros Georgiou <alex.georgiou@gmail.com>
*/
$address = urlencode( @$argv[ 1 ] );
define( 'API_KEY', 'GetYourOwnKeyAtTheLinkProvided' ); // https://developers.google.com/places/web-service/get-api-key
if ( $address ) {
$place_query = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={$address}&inputtype=textquery&key=" . API_KEY ;
$response_str = file_get_contents( $place_query );
if ( $response_str ) {
$response = json_decode( $response_str );
if ( $response ) {
print_r( $response );
foreach ( $response->candidates as $candidate ) {
$place_id = $candidate->place_id;
$place_id_query = "https://maps.googleapis.com/maps/api/place/details/json?placeid={$place_id}&key=" . API_KEY;
$response_str = file_get_contents( $place_id_query );
if ( $response_str ) {
$response = json_decode( $response_str );
if ( $response ) {
print_r( $response );
}
}
}
}
}
} else {
echo "Usage: php {$argv[0]} \"address string\"\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment