sachinagarwal1337 / google-places-api Goto Github PK
View Code? Open in Web Editor NEWThis is a PHP wrapper for Google Places API Web Service. And is Laravel Framework friendly.
License: MIT License
This is a PHP wrapper for Google Places API Web Service. And is Laravel Framework friendly.
License: MIT License
Hi, when I tried to insert more than one keywords ( making an array to the keyword), i didn't get the results that i wanted. It returned the keyword has no input. For example, I want to find nearest kfc and post office. But it returned all the nearest places without any specification.
$place = 'kfc';
$place2 = 'post office';
$keyword = array('keyword' => [$place,$place2]);
$lat = "2.9960560";
$lng = "101.5755560";
$location = $lat. "," .$lng;
$response = GooglePlaces::nearbySearch($location, $radius,$keyword);
I already tried the method below, it works, but it consumes a lot of API request. so i want to prevent that method.
$place = 'kfc';
$keyword = array('keyword' => $place);
$place2 = 'pos laju';
$keyword2 = array('keyword' => $place2);
$response = GooglePlaces::nearbySearch($location, $radius,$keyword);
response2 = GooglePlaces::nearbySearch($location, $radius,$keyword2);
$result = $response["results"]->merge($response2["results"]);
Hi, I installed this package and think it's great but I am having a problem providing the parameters to the placeAutocomplete method? I tried several different ways to pass the correct information but without any luck. I was hoping there was an example somewhere I missed with a more in depth example or could one be provided? Thanks for the help.
According to composer.json, PHP 5.6 is supported, however, I'm getting a syntax error.
Parse error: syntax error, unexpected '?' in ..../vendor/skagarwal/google-places-api/src/PlacesApi.php on line 258
The code is using the null coalescing operator which was added in PHP 7.0.
Hello,
Thank you for this laravel library, it helped me a lot. But there is an issue which i wanted to point out that i was facing, in the radar search i was sending one param of the three ('keyword', 'name', 'type') which is the requirement from google, but i always got an exception, saying "Radar Search require one or more of 'keyword', 'name', or 'types' params.". The case was when i provide all three params only then the call was successful, so I dug in a little into your code and found out that the method you have 'array_any_keys_exists' has a condition that will only be true if all keys passed are present in the params array, so i changed the code for it with something like:
function array_any_keys_exists(array $keys, array $array)
{
$result = false;
foreach ($keys as $key) {
if(array_key_exists($key, $array)){$result = true;break;}
else{$result = false;}
}
return $result;
}
After this change the radar search call executed with any one required param that is passed, same goes for the case of nearby search, your method search for all these three keys, when in fact only one passed should work also. Please let me know your thoughts on this and do correct me if I'm wrong, thank you for your time.
Regards,
Kamil
Hi there,
I was following the documentation for installing the places api on laravel 5.5 and i keep getting the "API KEY is not specified." error. I already added the api key in the google.php file, but still getting the same error.
Any idea of what i did wrong during install?
for the test i am trying to run this code in my show.blade.php
{{ GooglePlaces::placeAutocomplete('Jerusalem') }}
I am trying to make it possible for my application to fetch information about a business from Google. This means the business owner who has an account on my application can decide to populate the fields in my application by using Google but I keep running into this issue:
message: "Response returned with status: OVER_QUERY_LIMIT↵1",…
How do I fix it?
Hi, When trying to use the following module, I come across the following error coming from Slim 3:
{
"code": 0,
"message": "Middleware must return instance of \Psr\Http\Message\ResponseInterface",
"file": "C:\wamp64\www\test\vendor\slim\slim\Slim\MiddlewareAwareTrait.php",
"line": 75,
"trace": [
"#0 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(122): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#1 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#2 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\App.php(476): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#3 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(122): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#4 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\App.php(370): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#5 C:\wamp64\www\ff-api-php\vendor\slim\slim\Slim\App.php(295): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))",
"#6 C:\wamp64\www\ff-api-php\app\server.php(34): Slim\App->run()",
"#7 C:\wamp64\www\ff-api-php\public\index.php(7): require('C:\\wamp64\\www\\v...')",
"#8 {main}"
]
}
It seem that something with the guzzle Http PSR-7 makes the slim to having problems,
Any idea?
I want to have a city search for germany only but when I search for berlin the first result in autocomplete is berlin in new york.
I can set it directly in the link in google places api.
Hey,
This doesn't seem to work :
function array_any_keys_exists(array $keys, array $array)
{
$result = false;
foreach ($keys as $key) {
$result = $result OR array_key_exists($key, $array);
}
return $result;
}
changing this line : $result = $result OR array_key_exists($key, $array);
to
if (array_key_exists($key, $array)) return true;
solves the issue .
Are there plan to update the BASE_URL from public const BASE_URL = 'https://maps.googleapis.com/maps/api/place/';
to the new Google Places API base URL: https://places.googleapis.com/v1/places:searchText
(Migrate to Text Search (New))?
Hi
I am willing to use this in one my Codeigniter application,
Can you please share a sample in php which will do a text search
public function textSearch($query, $params = [])
I tried to use code but giving me error Parse error: syntax error, unexpected '$response'
As we know there already Laravel 6 on the market, Try to use this package but it always returning error
Hi,
Thanks for the package. Please implement support for Laravel 7+
When searching using textSearch for text that didn't show any result in google :
Fatal error: Uncaught SKAgarwal\GoogleApi\Exceptions\GooglePlacesApiException: Response returned with status: ZERO_RESULTS in /var/www/html/testing/vendor/skagarwal/google-places-api/src/PlacesApi.php:217 Stack trace: #0 /var/www/html/testing/vendor/skagarwal/google-places-api/src/PlacesApi.php(86): SKAgarwal\GoogleApi\PlacesApi->makeRequest('textsearch/json', Array) #1 /var/www/html/testing/search.php(8): SKAgarwal\GoogleApi\PlacesApi->textSearch('MY SEARCH QUERY...') #2 {main} thrown in /var/www/html/testing/vendor/skagarwal/google-places-api/src/PlacesApi.php on line 217
is there any way to handle the zeo result so it wont get fatal error?
How title says, is possible determine the km between 2 cities?
for example:
Point A: Montevideo, Uruguay
Point B: New York, USA
Distance between A to B: 300km
it's possible?
UPDATE: I think its possible with google geocoder, its implemented on this api?
Check this package: https://github.com/malhal/Laravel-Geographical
it would be awesome if you implements same in your package.
Hi, can anyone give an example how to use params? I tried it but the results does not reflect it:
$response = $googlePlaces->nearbySearch('11.585125,104.863643', 500,array('type=restaurant'));
Hi,
Thanks for the package, it's awesome!
One thing I haven't been able to work out is whether there is support for retrieving additional fields like ratings, opening hours and reviews for places?
If it does then I apologise for creating this issue.
If someone can confirm that this feature is absent then I can have a look into how viable it would be to add it against the Google documentation and can look at contributing to the package if others will find it useful.
Thanks,
Blessing
Hi,
$googlePlaces = new PlacesApi('xxx');
$collection = collect();
$response = $googlePlaces->textSearch('xxx', [
'type' => 'xxx'
]);
$collection->push($response);
$response = $googlePlaces->textSearch('xxx', [
'pagetoken' => $response->get('next_page_token'),
]);
$collection->push($response);
dd($collection);
All returns:
SKAgarwal \ GoogleApi \ Exceptions \ GooglePlacesApiException
Response returned with status: INVALID_REQUEST
First query works perfectly and has a next_page_token
. As long as I try the second query I got the error.
Can you please help?
Best,
J.
I am open for suggestions for adding new features or enhancements.
Thank you.
Hope you all like this small package.
Hi.
I can't understand, why I get error "API KEY is not specified"!
In config/google.php API KEY is present.
Help please.
Thanks.
If you want to use next_page token, so the next function should be modified, notice that I only added a condition.
and you should send the next param
'pagetoken' => 'all the code for the next page should be here'
public function nearbySearch($location, $radius = null, $params = [])
{
$this->checkKey();
if (!array_key_exists('pagetoken', $params)){
$params = $this->prepareNearbySearchParams($location, $radius, $params);
}
$response = $this->makeRequest(self::NEARBY_SEARCH_URL, $params);
return $this->convertToCollection($response, 'results');
}
HI
Keep getting request denied on following the setup process, running laravel 8 and tried using the facade and without the facade.
use GooglePlaces;
$response = GooglePlaces::placeAutocomplete('address', [
'headers' => [
'referer' => env('APP_URL')
]
]);
or
use SKAgarwal\GoogleApi\PlacesApi;
$googlePlaces = new PlacesApi(config('google.places.key'));
$response = GooglePlaces::placeAutocomplete('address', [
'headers' => [
'referer' => env('APP_URL')
]
]);
Both ways i keep getting Response returned with status: REQUEST_DENIED.
I created a new key with no restriction on it still getting that error.
Thanks
How can the error be handed when no results are found?
For example - if address = 'sdfgdsgdthrtehdrsgstgsdgs##$$#dfg'
$response = $googlePlaces->placeAutocomplete($request->input('address'),['types'=>'geocode']);
Thanks!
I have the following code:
$resp = $googlePlaces->nearbySearch('11.585125,104.863643', 500,array('type' => 'school'));
How do i iterate over $resp?
Hello
In step 3 should be
$response = GooglePlaces::placeAutocomplete('some city');
instead of
$response = GooglePlaces::placesAutocomplete('some city');
Please, consider updating composer.json
file to
"require": {
"php": "^5.5.9|^7.2|^7.3|^8.0",
...
}
in order to support PHP 8
Please fix some mistakes in readme:
composer require skagarwal/google-places-api
typo in requrire
php artisan vendor:publish --provider="SKAgarwal\GoogleApi\ServiceProvider"
quotes required
Hi, thx for making the convenient package.
I just want thing to be added to the official doc.
To use with Laravel 5.5 above with the auto discovery feature, this line needs to be added for the usage:
use GooglePlaces;
I found this after a period of Google search:
https://laracasts.com/discuss/channels/general-discussion/a-new-open-source-package-for-google-places-api
And also add
GOOGLE_PLACES_API_KEY=YOUR_API_KEY
to .env
I don't see these in the documentation. And I think they should be part of the official doc.
I cannot seem to get the facade working in Laravel 10, or at least code completion in PHPStorm.
I get this error:
Class "App\Http\Livewire\Company\GooglePlaces" not found
When running:
$this->apiStuff = GooglePlaces::placeAutocomplete('some city');
I can get GooglePlaces::placeAutocomplete('some city');
to work from other parts of my app (ie., web.php), but code completion doesn't seem to work.
The documented, non-laravel works as documented.
use SKAgarwal\GoogleApi\PlacesApi;
function () {
$googlePlaces = new PlacesApi('API_KEY') # line 1
$response = $googlePlaces->placeAutocomplete('some input'); # line 2
}
Noticed a couple of typos and issues with the handling of radar search parameters
The docs aren't completely clear about how to use laravel collections with results... I've had to find out the hard way that the results list is buried in $response->get('results') . For placeDetails, it's $response->get('result') .
From there, $response->get('results')->each(), or any other list/loop method in Collection works.
It would be helpful to update the docs to be more detailed.
Hi, Is there a way to get the second page using the "next_page_token" from the backend?
Here's my code.
$googlePlaces = new PlacesApi($this->getGoogleApi());
$params = [
'keyword' => $keyword,
'name' => null,
'types' => null
];
$responses = $googlePlaces->nearbySearch($location, $radius = '30000', $params);
But I want to get the second page using the "$responses['next_page_token']"
Thank You.
Dears,
I'm doing an app that needs Google Places API. I'm using Laravel 6.x. Does this work good with this version? I haven't saw any question or taking about the interation for this package with Laravel 6.x until this moment.
Hello,
I'm coming into an issue with my production key: Response returned with status: REQUEST_DENIED
.
From what I see, it could come from the referer security.
I've checked the package method, vendor/skagarwal/google-places-api/src/PlacesApi.php::makeRequest()
, and it uses a Guzzle Client
to do the request to Google.
From Guzzle documentation, we can passes headers like this:
$client->request('GET', '/get', [
'headers' => [
'X-Foo' => ['Bar', 'Baz']
]
]);
We can pass a 2nd parameter to the $placeApi->placeAutocomplete()
method, but this parameter is passed trought the vendor/skagarwal/google-places-api/src/PlacesApi.php::getOptions()
method.
Reading the getOptions()
code, my solution to pass a ['headers' => ['referer' => config('app.url')]]
as second parameter of placeAutocomplete()
could only work with a POST as our array is top level merged with the $options
variable. But when it's a GET, it's merged in the array key $options['query']
. So it's passed like this to Guzzle, and so not work (not recognized as the header parameter).
Is there any solution to pass custom headers to the request?
Thanks.
The nearbySearch
method returns results, and I am able to retrieve them fine, but I wanted to use the address fields of the place documented in the Google Places API documentation for nearby search results.
But, among others address_components
, adr_address
, formatted_address
. are missing in the results.
The only fields returned are:
I am doing the following:
$googlePlaces = new PlacesApi($this->api_key);
$params = [
'keyword' => 'restaurant',
'name' => null,
'types' => null,
'pagetoken' => $next_page_token
];
$response = $googlePlaces->nearbySearch($this->location, $this->radius, $params);
$results = $response->all()['results']->all();
var_dump($response);
Am I missing something? How can I get the address details of a place in the results of the nearby search? Of course I can do another api call and get those data but we're doing a large amount of queries so that would double our api usage.
Is it possible to temporarily set the 'verify' => false
option in the Guzzle client before or while I call the google-places-api? (I need this on my dev machine)
Right now I changed PlacesApi.php:
$this->client = new Client([
'base_uri' => 'https://maps.googleapis.com/maps/api/place/',
'verify' => false
]);
But of course I don't want to touch those files.
Maybe an optional second param for guzzle client options is a way?
$p = new SKAgarwal\GoogleApi\PlacesApi('KEY', []);
Thanks!
You get SSL certificate error even when you set $verifySSL = false
at the constructor.
This is because it was never set, so it always defaults to true as shown in this snippet irrespective of what you set it to be
/**
* PlacesApi constructor.
*
* @param null $key
* @param bool $verifySSL
*/
public function __construct($key = null, $verifySSL = true)
{
$this->key = $key;
$this->client = new Client([
'base_uri' => self::BASE_URL,
]);
}
I'll be making a pull request to address this.
Our project hasn't caught up to the latest version of Laravel/PHP yet, When we tried to deploy our application on our staging servers. We problem with running composer install
(see image below).
skagarwal/google-places-api 1.5.0
on a PHP 7.1 environmentChange dependencies to allow lower PHP versions as it only currently allows ^5.5.9|^7.2
google-places-api/composer.json
Line 21 in da43d9b
Hi,
When I'm trying to get only cities and countries:
[
'types' => '(cities)',
]
For some reason, I can't get city lat and long. In the JS version lat and long are exists. Then how I can get those details in the response?
1.3
5.8
$place_id = 'ChIJj6ARWEZRCTERsHO6sP6sYD4';
$params = ['fields' => 'formatted_phone_number'];
return \GooglePlaces::placeDetails($place_id, $params);
https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJj6ARWEZRCTERsHO6sP6sYD4&fields = formatted_phone_number
Updated: Sorry ! that my silly mistake with cache
As far as I can tell, currently there is only the GooglePlacesApiException. To decide what exactly went wrong one currently has to parse the Exception message. In my opinion there should be more specific Exceptions so that one is able to catch and handle them separately. Even faulty responses should get separate Exceptions by response status (i.e.: GooglePlacesApiRequestDeniedException, GooglePlacesApiInvalidRequestException, ...).
Hi,
How can i get the My Google business reviews, using the package for Laravel.
Thanks
Hi, sorry for being a dunce. Here is my code right now:
$response = New PlacesApi(env('GEOCODE_GOOGLE_APIKEY'));
$response->placeAutocomplete('90210');
and here is the fully expanded result that I'm getting
PlacesApi {#281 ▼
+status: "OK"
-key: "AIzaSyD-mqqpxT84kbJ6B5fhWlNIrx9KwSn-DhM"
-client: Client {#276 ▼
-config: array:8 [▼
"base_uri" => Uri {#294 ▼
-scheme: "https"
-userInfo: ""
-host: "maps.googleapis.com"
-port: null
-path: "/maps/api/place/"
-query: ""
-fragment: ""
}
"handler" => HandlerStack {#277 ▼
-handler: Closure {#289 ▼
class: "GuzzleHttp\Handler\Proxy"
parameters: {▼
$request: {▼
typeHint: "Psr\Http\Message\RequestInterface"
}
$options: {▼
typeHint: "array"
}
}
use: {▼
$default: Closure {#287 …}
$streaming: StreamHandler {#288 …}
}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php"
line: "49 to 53"
}
-stack: array:4 [▼
0 => array:2 [▼
0 => Closure {#290 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "55 to 70"
}
1 => "http_errors"
]
1 => array:2 [▼
0 => Closure {#291 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "149 to 151"
}
1 => "allow_redirects"
]
2 => array:2 [▼
0 => Closure {#292 ▼
class: "GuzzleHttp\Middleware"
parameters: {▶}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "27 to 44"
}
1 => "cookies"
]
3 => array:2 [▼
0 => Closure {#293 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "217 to 219"
}
1 => "prepare_body"
]
]
-cached: Closure {#303 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$request: {}
$options: {▼
typeHint: "array"
}
}
use: {▼
$handler: RedirectMiddleware {#302 …}
}
file: "/Users/mick/Dropbox/Projects/getfed20/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "56 to 69"
}
}
"allow_redirects" => array:5 [▼
"max" => 5
"protocols" => array:2 [▼
0 => "http"
1 => "https"
]
"strict" => false
"referer" => false
"track_redirects" => false
]
"http_errors" => true
"decode_content" => true
"verify" => true
"cookies" => false
"headers" => array:1 [▼
"User-Agent" => "GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.8"
]
]
}
-verifySSL: true
}
I don't know what I'm doing wrong.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.