Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.