I am trying to include guzzle http client from a vendor folder and using composer. Here is what I have tried so far.
Location of guzzle http client file vendor/guzzle/guzzle/src/Guzzle/Http/Client.php
In composer.json file I included
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"files":["vendor/guzzle/guzzle/src/Guzzle/Http/Client.php"],
"psr-4": {
"App\\": "app/"
}
},
The I ran the command composer dumpautoload
.
In my controller I am trying to call an api end point like this
use GuzzleHttp\Client;
$client = new Client(); // this line gives error
$res = $client->get('https://api.fixer.io/latest?symbols=CZK,EURO');
The error is Class 'GuzzleHttp\Client' not found
What I am missing here, please help me. Thanks.
For a better file structure here is a screenshot of of the file location
Laravel's Vendor directory The vendor directory contains the composer dependencies, for example, to install Laravel setup, the composer is required. The vendor folder contains all the composer dependencies.
The vendor is a subfolder in the Laravel root directory. It includes the Composer dependencies in the file autoload. php. Composer is a PHP based tool for dependency management.
Short Version: You're trying to instantiate a class that doesn't exist. Instantiate the right class and you'll be all set.
Long Version: You shouldn't need to do anything fancy with your composer.json to get Guzzle working. Guzzle adheres to a the PSR standard for autoloading, which means so long as Guzzle's pulled in via composer, you can instantiate Guzzle classes without worrying about autoloading.
Based on the file path you mentioned, it sounds like you're using Guzzle 3. Looking specifically at the class you're trying to include
namespace Guzzle\Http;
/*...*/
class Client extends AbstractHasDispatcher implements ClientInterface
{
/*...*/
}
The guzzle client class in Guzzle 3 is not GuzzleHttp\Client
. Its name is Guzzle\Http\Client
. So try either
$client = new \Guzzle\Http\Client;
or
use Guzzle\Http\Client;
$client = new Client;
and you should be all set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With