geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.95k stars 516 forks source link

[Google Maps] API access denied #975

Closed ghost closed 5 years ago

ghost commented 5 years ago

My code:

<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client as GuzzleClient;
use Geocoder\Provider\GoogleMaps;
use Http\Adapter\Guzzle6\Client;
use Geocoder\Query\GeocodeQuery;

$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient,$key ='AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s');
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
$result = $geocoder->geocodeQuery(GeocodeQuery::create('5 Place des Palais 1000 Bruxelles'));
$query = GeocodeQuery::create('5 Place des Palais 1000 Bruxelles')
    ->withData('streetNumber', '5')
    ->withData('streetName', 'Place des Palais')
    ->withData('postalCode', '1000')
    ->withData('locality', 'Bruxelles');
$results = $geocoder->geocodeQuery($query);
  ?>

the error thrown by the API: Uncaught Geocoder\Exception\InvalidServerResponse: API access denied. Request: https://maps.googleapis.com/maps/api/geocode/json?address=5%20Place%20des%20Palais%201000%20Bruxelles&language=en&region=AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s - Message: You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account

I tried passing the API key as a parameter but it isn't working.

ghost commented 5 years ago

I fixed it by doing the following:

<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client as GuzzleClient;
use Geocoder\Provider\GoogleMaps;
use Http\Adapter\Guzzle6\Client;
use Geocoder\Query\GeocodeQuery;

$apikey = 'AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s';

$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'Brazil', $apikey);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
$result = $geocoder->geocodeQuery(GeocodeQuery::create('240, Rua Clóvis da Fonseca'));
var_export($result);

  ?>
ghost commented 5 years ago

Response of the API: Geocoder\Model\AddressCollection::__set_state(array( 'locations' => array ( 0 => Geocoder\Provider\GoogleMaps\Model\GoogleAddress::__set_state(array( 'id' => 'ChIJYfQbLWSZ7JQRHXNPh43uAuQ', 'locationType' => 'ROOFTOP', 'resultType' => array ( 0 => 'street_address', ), 'formattedAddress' => 'R. Clóvis da Fonseca, 240 - Centro, Apucarana - PR, 86800-110, Brazil', 'streetAddress' => NULL, 'intersection' => NULL, 'political' => 'Brazil', 'colloquialArea' => NULL, 'ward' => NULL, 'neighborhood' => NULL, 'premise' => NULL, 'subpremise' => NULL, 'naturalFeature' => NULL, 'airport' => NULL, 'park' => NULL, 'pointOfInterest' => NULL, 'establishment' => NULL, 'subLocalityLevels' => Geocoder\Model\AdminLevelCollection::__set_state(array( 'adminLevels' => array ( 1 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 1, 'name' => 'Centro', 'code' => 'Centro', )), ), )), 'partialMatch' => false, 'coordinates' => Geocoder\Model\Coordinates::__set_state(array( 'latitude' => -23.5511145, 'longitude' => -51.4571663, )), 'bounds' => Geocoder\Model\Bounds::__set_state(array( 'south' => -23.5524634802915, 'west' => -51.45851528029151, 'north' => -23.5497655197085, 'east' => -51.45581731970851, )), 'streetNumber' => '240', 'streetName' => 'Rua Clóvis da Fonseca', 'subLocality' => 'Centro', 'locality' => NULL, 'postalCode' => '86800-110', 'adminLevels' => Geocoder\Model\AdminLevelCollection::__set_state(array( 'adminLevels' => array ( 1 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 1, 'name' => 'Paraná', 'code' => 'PR', )), 2 => Geocoder\Model\AdminLevel::__set_state(array( 'level' => 2, 'name' => 'Apucarana', 'code' => 'Apucarana', )), ), )), 'country' => Geocoder\Model\Country::__set_state(array( 'name' => 'Brazil', 'code' => 'BR', )), 'timezone' => NULL, 'providedBy' => 'google_maps', )), ), ))

jbelien commented 5 years ago

This is not a correct way to pass parameters to a function in PHP.

$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient,$key ='AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s');

This is the correct way:

$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, 'AIzaSyBbiute7hwz_ESi3U75ETeRwgnjfceZm3s');

Other remark : I suggest you generate a new API key because everyone knows your API key. Don't publish secret API keys !

ghost commented 5 years ago

Thanks @jbelien, I know, i was in despair because I will use the project in our map display, on my job, I will change the key.