geocoder-php / Geocoder

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

Uncaught TypeError: GuzzleHttp\Client given instead of Http\Client\HttpClient in GoogleMaps::__construct() #974

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hello I am using your API, and the recommended HttpAdapter, I'm using Guzzle, and installed geocoder and the adapter with Composer. When I make a request to the provider it throws this error:

Uncaught TypeError: Argument 1 passed to Geocoder\Provider\GoogleMaps\GoogleMaps::__construct() must be an instance of Http\Client\HttpClient, instance of GuzzleHttp\Client given, called in D:\xampp\htdocs\Novo Mega Treino Michel\mapa.php on line 23 and defined in D:\xampp\htdocs\Novo Mega Treino Michel\vendor\geocoder-php\google-maps-provider\GoogleMaps.php:97 Stack trace: #0 D:\xampp\htdocs\Novo Mega Treino Michel\mapa.php(23): Geocoder\Provider\GoogleMaps\GoogleMaps->__construct(Object(GuzzleHttp\Client)) #1 {main} thrown in D:\xampp\htdocs\Novo Mega Treino Michel\vendor\geocoder-php\google-maps-provider\GoogleMaps.php on line 97
<?php
require_once('vendor/autoload.php');
ini_set('display_errors', 'On');
use GuzzleHttp\Client;
use Geocoder\Provider\GoogleMaps;

include('Guzzle6HttpAdapter.php');
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

// Send a request to https://foo.com/api/test

$response = $client->request('GET', 'https://httpbin.org/get');
$response = $client->post('http://httpbin.org/post');
$code = $response->getStatusCode();
echo $code . "</br>";
echo $response->getHeader('Content-Length')[0];

$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($client);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');

$teste  =  var_dump($result);

$result = $geocoder->reverseQuery(ReverseQuery::fromCoordinates('43.0481','-76.1474'));

$dumper = new \Geocoder\Dumper\Gpx();
$strGpx = $dumper->dump($teste);

echo $strGpx;
 ?>
atymic commented 5 years ago

Please edit the title to remove the trace.

You need to create a HTTPplug guzzle client instance, not a guzzle client Use the following code:

$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
jbelien commented 5 years ago

@maschinedreams I guess your issue is fixed ? May I close this issue ?

ghost commented 5 years ago

Hey @atymic, thanks for the help. I tested and it worked, I was seeing code on and on, but the docs are very little informative, if I have any issue can I talk with you guys?

jbelien commented 5 years ago

No worries, we are here to help.