Created
July 16, 2019 09:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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