在Laravel 5中安装Guzzle

16
如何将 Guzzle 安装到 Laravel 5 中? 我正在使用 Laravel 进行项目开发,但我需要像 Guzzle 这样的库来帮助我在 Laravel 中轻松使用 curl。有人能提供帮助吗?
6个回答

36

打开终端,进入你的 Laravel 项目根目录并输入:

composer require guzzlehttp/guzzle

或者,您可以添加

"guzzlehttp/guzzle":"*"
将以下内容添加到你的 composer.json 文件的 require 部分并运行 composer update。

to your composer.json file's require section and run composer update.


如何将Guzzle实现为库或助手? - Rahman
实现和调用控制器 - Rahman
1
@baao 这应该更新为 composer require guzzlehttp/guzzle - 因为 composer require guzzle/guzzle 已经被废弃 - 请参见 https://packagist.org/packages/guzzle/guzzle - goredwards
我在这里绞尽脑汁... 我得到了“Class 'Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory' not found”。 - MiloTheGreat

9
通过Composer,在你的Laravel项目根目录下,执行以下命令:
composer require guzzlehttp/guzzle

就是这样。现在 Guzzle 已经安装并且可以使用了。


3

添加到composer.json要求中

"guzzlehttp/guzzle": "5.*"

5.*是Guzzle的版本,它可能会发生变化,更多信息请参见Guzzle的GitHub资料。

编辑后运行:

composer update

欲了解更多,请参阅Guzzle


2

由于Guzzle是一个通用的PHP包,而不是专门为Laravel构建的,因此对Laravel用户来说有点困惑,因为您无法“静态”使用类函数。

要在Laravel 5中安装和使用Guzzle(我在Laravel 5.7中使用它),

composer require guzzlehttp/guzzle

然后您应该在vendor文件夹中看到guzzlehttp文件夹。

要使用它,您可以

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client as GuzzleClient;
...
public function testGuzzle()
{
    $client = new GuzzleClient();
    ...
}

如果您不想导入命名空间,也可以直接按以下方式使用

$client = new \GuzzleHttp\Client();

如前所述,您不能以“静态”的方式使用它

GuzzleClient::request('GET', 'https://api.xxxx'); // this will throw you error.

0

在你的composer.json文件中添加到require中:

"guzzlehttp/guzzle": "~5.0"

保存并更新您的Composer。


0

通过使用以下存储库https://github.com/Bogardo/Mailgun,可以轻松实现此目标。

我相信上面的链接在使用guzzlehttp 5.3 ~ 6.0时不会有任何问题。

但是,如果您正在使用guzzle版本高于6.0的Oauth,请比较上述链接和下面的“/composer.json”、“/src/Bogardo/Mailgun/Mailgun/MailgunApi.php”文件。https://github.com/milocosmopolitan/Mailgun


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接