php 如何在 Laravel 4 中自动加载 Guzzle?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22725266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to autoload Guzzle in Laravel 4?
提问by Iain
How can I autoload Guzzlein Laravel 4?
如何在 Laravel 4 中自动加载Guzzle?
I am encountering the following error when I try to create a new GuzzleHttp/Client:
当我尝试创建新的 GuzzleHttp/Client 时遇到以下错误:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'GuzzleHttp\Client' not found
I have the following set up in my composer.json autoload section:
我在我的 composer.json 自动加载部分设置了以下内容:
autoload: {
"psr-0": {
"Guzzle\": "src/"
}
}
回答by Antonio Carlos Ribeiro
You don't need to add Guzzle to your composer.json, it's already autoloaded by it's own composer.json.
您不需要将 Guzzle 添加到您的 composer.json,它已经由它自己的 composer.json 自动加载。
Guzzle 4
狂饮4
PHP 5.4.x+ required
需要 PHP 5.4.x+
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
狂饮3
Install it:
安装它:
composer require "guzzle/guzzle" "~3.0"
Create a client setting the base URL:
创建一个设置基本 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:
如果您仍然无法运行,请检查该文件vendor/composer/autoload_psr4.php
,Guzzle 必须出现在其中。如果没有,请删除您的供应商文件夹并重新安装:
rm -rf vendor
rm composer.lock
composer install