Stars: 178
Forks: 47
Pull Requests: 31
Issues: 44
Watchers: 12
Last Updated: 2023-02-03 10:30:03
This is a PHP wrapper for Google Places API Web Service. And is Laravel Framework friendly.
License: MIT License
Languages: PHP
This is a PHP wrapper for Google Places API Web Service. And is Laravel Framework friendly.
With just 2 lines of code you can request to any google places api feature. No need to manually perform any curl requests.
Install it with composer
composer require skagarwal/google-places-api
Laravel user can see the Laravel Usage section
use SKAgarwal\GoogleApi\PlacesApi;
$googlePlaces = new PlacesApi('API KEY');
Note: You can also set the API KEY after initiating the class using setKey('KEY')
method. You can chain this with method with any other methods.
Example:
$response = $googlePlaces->placeAutocomplete('some Place');
As mentioned earlier just 2 lines of code to make any request.
Full example:
use SKAgarwal\GoogleApi\PlacesApi;
function () {
$googlePlaces = new PlacesApi('API_KEY') # line 1
$response = $googlePlaces->placeAutocomplete('some input'); # line 2
}
Auto Discovery added.
Set up the service provider and facade in the config\app.php
'providers' => [
....
....
SKAgarwal\GoogleApi\ServiceProvider::class,
];
'aliases' => [
....
....
'GooglePlaces' => SKAgarwal\GoogleApi\Facade::class,
];
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 using the package using Facade.
$response = GooglePlaces::placeAutocomplete('some city');
The response returned is a Laravel's Collection so that you can perform any of the available collection methods on it.
If you are not familiar with Laravel's Collection you can either reference the docs here or you can use response as simple array.
location
— The latitude/longitude around which to retrieve place information. This must be specified as latitude, longitude.radius
must not be included if rankby=distance
(described under Optional parameters below) is specified.rankby=distance
(described under Optional parameters below) is specified, then one or more of keyword
, name
, or types
is required.params
- Optional Parameters You can refer all the available optional parameters on the Google's Official Webpagequery
— The text string on which to search, for example: "restaurant". The Google Places service will return candidate matches based on this string and order the results based on their perceived relevance.params
- Optional Parameters You can refer all the available optional parameters on the Google's Official Webpageinput
— The text input specifying which place to search for (for example, a name, address, or phone number).inputType
— The type of input. This can be one of either textquery or phonenumber. Phone numbers must be in international format (prefixed by a plus sign ("+"), followed by the country code, then the phone number itself).params
- Optional Parameters You can refer all the available optional parameters on the Google's Official WebpageplaceId
— A textual identifier that uniquely identifies a place, returned from a Place Search.params
- Optional Parameters You can refer all the available optional parameters on the Google's Official Webpageinput
— The text string on which to search. The Place Autocomplete service will return candidate matches based on this string and order results based on their perceived relevance.params
- Optional Parameters You can refer all the available optional parameters on the Google's Official Webpageinput
— The text string on which to search. The Places service will return candidate matches based on this string and order results based on their perceived relevance.params
- Optional Parameters You can refer all the available optional parameters on the Google's Official Webpageparams
- The set of key-value parameters necessary to add a place to Google. You can refer to the fields on Google's Official Webpage regarding Place AddCall This method before any other methods to set the headers. You can chain this method.
To have custom headers set for every call, you can pass 3rd parameter as the headers to class constructor.
Note: For Laravel Users, you can set this in config file with key headers
This will return the status of the response send by google api. Use it after making any request.
This will return the API KEY
been used with the requests.
This will set the API KEY
.
You can pass false
to disable Verification of SSL Certification.
Note: For Laravel Users, you can set this in config file with key verify_ssl
Or You can Pass the path to the certificate.
Google Places API may throw various exceptions based on the given $params
or response and is located in the SKAgarwal\GoogleApi\Exceptions
namespace.
GooglePlacesApiException
is thrown when no API KEY
is provided or $params
is invalid.
Note: This is the parent class for the preceding exceptions.InvalidRequestException
is thrown when the response status
is INVALID_REQUEST
OverQueryLimitException
is thrown when the response status
is OVER_QUERY_LIMIT
RequestDeniedException
is thrown when the response status
is REQUEST_DENIED
UnknownErrorException
is thrown when the response status
is UNKNOWN_ERROR
NotImplementedException
is thrown when the response cannot be determined.If any of these exception has been thrown, you can use the getErrorMessage()
method to get the error_message
field from the response if any is provided.
Note: error_message
field is not guaranteed to be always present, and its content is subject to change.
Feel free to report issues or make Pull Requests. If you find this document can be improved in any way, please feel free to open an issue for it.
The Google Places Api is open-sourced software licensed under the MIT license