27

如何在 Laravel 4 中自动加载Guzzle

当我尝试创建新的 GuzzleHttp/Client 时遇到以下错误:

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

我在 composer.json 自动加载部分进行了以下设置:

autoload: {
    "psr-0": {
        "Guzzle\\": "src/"
    }
}
4

1 回答 1

45

您不需要将 Guzzle 添加到您的 composer.json,它已经由它自己的 composer.json 自动加载。

狂饮 4

需要 PHP 5.4.x+

composer require "guzzlehttp/guzzle" "~4.0"

创建客户端:

$client = new \GuzzleHttp\Client();

获取结果:

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

dd($response->getBody());

狂饮 3

安装它:

composer require "guzzle/guzzle" "~3.0"

创建一个设置基本 URL 的客户端:

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

得到你的回应:

$username = 'antonioribeiro';

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

并显示它:

dd($response);

如果您仍然无法运行,请检查文件vendor/composer/autoload_psr4.php,Guzzle 必须出现在其中。如果没有,请删除您的供应商文件夹并重新安装:

rm -rf vendor
rm composer.lock
composer install
于 2014-03-29T00:40:52.477 回答