skagarwal/google-places-api
PHP wrapper for Google Places API Web Service with Laravel support. Includes Places API and Places API (New) endpoints like autocomplete/search, built on Saloon in v3. Composer install, fluent client setup, configurable SSL and error handling.
A PHP wrapper for Google Places API Web Service, compatible with Laravel.
| Package Version | PHP | Laravel | Google Places API |
|---|---|---|---|
| ^3.0 | ^8.1 | ^10 | ^11 | Places API and Places API (New) |
| ^2.2 | ^8.0.2 | ^9 | ^10 | ^11 | Places API |
[!CAUTION] Version 3 is a complete rewrite using Saloon with breaking changes.
v2.2 API is deprecated and will be removed in next major version. Which means it is backward compatible.
And it is recommended to shift to v3 API before next major release.
You can refer to v2.2 documentation here.
Install it with composer
composer require skagarwal/google-places-api
Laravel user can see the Laravel Usage section
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces;
public function foo() {
$response = GooglePlaces::make(key: 'API KEY', verifySSL: false, throwOnError: false)->autocomplete('some input');
$data = $response->array();
}
You can also set the API KEY after initiating the class using
GooglePlaces::make()->setKey('KEY')method.
publish the config file with following artisan command
php artisan vendor:publish --provider="SKAgarwal\GoogleApi\ServiceProvider"
This will create google.php file in the config directory.
Set the API KEY in this config file.
Start making requests
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces;
public function foo() {
$response = GooglePlaces::make()->autocomplete('some input');
$data = $response->collect();
}
The response returned is a Saloon's Response thus you can use all the methods provided by Saloon.
$response->array(); // returns the response as array
$response->collect(); // returns the response as collection
$response->json(); // returns the response as json
$response->status(); // returns the status of the response
$response->headers(); // returns the headers of the response
$response->body(); // returns the body of the response
$response->throw(); // throws an exception if the response is not successful
You can refer to Saloon's documentation for more methods.
[!IMPORTANT]
By default, no exception is thrown for API errors. You can check the request status with$response->status().
You can also useGooglePlaces::make()->findPlace()->throw()to throw an exception if the request fails.
This library supports both the Places API (original) and the Places API (New).
This section covers methods available for original Places API.
use SKAgarwal\GoogleApi\Places\GooglePlaces; // Original Places API class
public function foo() {
$response = GooglePlaces::make()->nearbySearch('40.748817,-73.985428');
$data = $response->array();
}
findPlace(string $input, string $inputType, array $params = [])textquery or phonenumber.nearbySearch(string $location, ?string $radius = null, array $params = [])40.748817,-73.985428).rankby=distance.keyword, type). More info.textSearch(string $query, array $params = [])"restaurant").placeDetails(string $placeId, array $params = [])placeAutocomplete(string $input, array $params = [])queryAutocomplete(string $input, array $params = [])photo(string $photoReference, array $params = [])This section covers methods available for Places API (New).
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces; // New Places API class
public function foo() {
$response = GooglePlaces::make()->nearbySearch(40.748817, -73.985428, 500.0);
$data = $response->array();
}
autocomplete(string $input, bool $includeQueryPredictions = false, ?array $fields = null, array $params = [])true, the response includes both place and query predictions. The default value is false, meaning the response only includes place predictions.nearbySearch(float $latitude, float $longitude, float $radius = 0.0, array $fields = ['*'], array $params = [])placeDetails(string $placeId, array $fields = ['*'], array $params = [])textSearch(string $textQuery, array $fields = ['*'], array $params = [])"restaurant").placePhoto(string $name, int $maxHeightPx = null, int $maxWidthPx = null)skipHttpRedirect is set to false internally to get JSON response. This cannot be changed
GooglePlaces::make()->headers()->add('Header-Key', 'Header-Value');
setKey(string $key): Set the API key.getKey(string $key): Get the API key being used.verifySSL(bool $verifySSL = true): Enable/disable SSL verification.throwOnErrors(bool $throwOnError):
$response->status().throwOnError is set to true, the library will throw exceptions on API failures.develop branch.This package is licensed under the MIT License.
How can I help you explore Laravel packages today?