【发布时间】:2015-10-22 20:21:18
【问题描述】:
如何在 Laravel 5 中安装 Guzzle? 我在我的项目中使用 laravel,但我需要像 guzzle 这样的库来让我在 laravel 中轻松使用 curl。任何机构都可以提供帮助?
【问题讨论】:
如何在 Laravel 5 中安装 Guzzle? 我在我的项目中使用 laravel,但我需要像 guzzle 这样的库来让我在 laravel 中轻松使用 curl。任何机构都可以提供帮助?
【问题讨论】:
打开一个终端,进入你的 laravel 项目根目录并输入
composer require guzzlehttp/guzzle
或者,您可以添加
"guzzlehttp/guzzle":"*"
到您的 composer.json 文件的 require 部分并运行 composer update。
【讨论】:
composer require guzzlehttp/guzzle - 因为composer require guzzle/guzzle 已被放弃 - 请参阅packagist.org/packages/guzzle/guzzle
这可以通过使用以下 repo 轻松完成 https://github.com/Bogardo/Mailgun
我相信上面的链接对于guzzlehttp 5.3 ~ 6.0不会有问题
但是,如果您使用 6.0 以上的 guzzle 版本的 Oauth, 比较以上链接和以下链接之间的“/composer.json”、“/src/Bogardo/Mailgun/Mailgun/MailgunApi.php”文件。 https://github.com/milocosmopolitan/Mailgun
【讨论】:
通过composer,cd进入你的laravel项目根目录,然后
composer require guzzlehttp/guzzle
就是这样。现在 guzzle 已安装并可以使用了。
【讨论】:
在 require 中添加到您的 composer.json 文件:
"guzzlehttp/guzzle": "~5.0"
保存然后更新您的作曲家。
【讨论】:
添加到 composer.json 要求中
"guzzlehttp/guzzle": "5.*"
(5.* 是 Guzzle 版本,可以在 guzzle github 配置文件中查看更多内容)
编辑运行后:
composer update
欲了解更多信息,请参阅Guzzle。
【讨论】:
由于 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.
【讨论】: