27

How can I autoload Guzzle in Laravel 4?

I am encountering the following error when I try to create a new GuzzleHttp/Client:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'GuzzleHttp\Client' not found

I have the following set up in my composer.json autoload section:

autoload: {
    "psr-0": {
        "Guzzle\\": "src/"
    }
}
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Iain
  • 1,724
  • 6
  • 23
  • 39

1 Answers1

45

You don't need to add Guzzle to your composer.json, it's already autoloaded by it's own composer.json.

Guzzle 4

PHP 5.4.x+ required

composer require "guzzlehttp/guzzle" "~4.0"

Create a client:

$client = new \GuzzleHttp\Client();

Get results:

$response = $client->get('http://api.github.com/users/antonioribeiro');

dd($response->getBody());

Guzzle 3

Install it:

composer require "guzzle/guzzle" "~3.0"

Create a client setting the base URL:

$client = new \Guzzle\Service\Client('http://api.github.com/users/');

Get your response:

$username = 'antonioribeiro';

$response = $client->get("users/$username")->send();

And display it:

dd($response);

If you still don't get it running, check the file vendor/composer/autoload_psr4.php, Guzzle must appear in it. If it doesn't, remove your vendor folder and install it again:

rm -rf vendor
rm composer.lock
composer install
Community
  • 1
  • 1
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Thanks Antonio, this works. This seems to differ from the documentation (or should I learn to rtfm properly?) as it suggests creating a new client by calling `use GuzzleHttp\Client` and then `$client = new Client()` which is what was giving me the errors. Can you enlighten me, please? – Iain Mar 29 '14 at 00:46
  • To clarify, the docs I was reading are located here http://docs.guzzlephp.org/en/latest/ – Iain Mar 29 '14 at 00:49
  • Oh! Looks like the docs are talking about the wrong namespace, somehow... But it could not be so wrong, right? I'll try to take a look at it later. – Antonio Carlos Ribeiro Mar 29 '14 at 01:14
  • 3
    Guzzle came up with the idea to rename the namespace they were using when going from version 3 (using `\Guzzle`) to 4 (using `\GuzzleHttp`). They say they did it to allow using both versions concurrently, which is technically correct, but in the end it behaves like a completely different product now. – Sven Mar 29 '14 at 02:09
  • And I am "-1" this, because NEVER EVER suggest installing branches like "dev-master" if you can by all means avoid it. Guzzle is an excellent example of a software using semantic versioning - there is absolutely no reason to not install a numbered version like `~4.0`. Additionally, using a version would make it clear that you are talking about 3.x instead of 4.x. – Sven Mar 29 '14 at 02:11
  • Thanks for your suggestion, Sven. I'm still in the early stages of my php+composer+laravel+webdevelopment learnship, so I would like to ask you to extend your lesson to Jeffrey Way, which may have used dev-master, unfortunatelly, in a dozen+ of Laracasts videos and he could be, this way, in the processes of confusing many more people like me. Changing (edited) to ~3.0 may have made it clear, but still downloaded guzzle and guzzlehttp dev-master, as you can see here: http://laravel.io/bin/Q5Qa. – Antonio Carlos Ribeiro Mar 29 '14 at 05:13
  • I'm using Guzzle that is installed with Laravel 4.0. The FQN for the client is `\Guzzle\Http\Client()` rather than `\GuzzleHttp\Client()`. This differs from the documentation for both Guzzle 3 and 4. – Jason Aug 15 '14 at 11:36