How to autoload Guzzle in Laravel 4?

24,776

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
Share:
24,776

Related videos on Youtube

Iain
Author by

Iain

Updated on July 09, 2022

Comments

  • Iain
    Iain almost 2 years

    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/"
        }
    }
    
    • The Alpha
      The Alpha about 10 years
      Where is this located ? Full path and how did you install it ?
  • Iain
    Iain about 10 years
    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
    Iain about 10 years
    To clarify, the docs I was reading are located here docs.guzzlephp.org/en/latest
  • Antonio Carlos Ribeiro
    Antonio Carlos Ribeiro about 10 years
    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.
  • Sven
    Sven about 10 years
    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
    Sven about 10 years
    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.
  • Antonio Carlos Ribeiro
    Antonio Carlos Ribeiro about 10 years
    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: laravel.io/bin/Q5Qa.
  • Jason
    Jason over 9 years
    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.