简体   繁体   English

在 Laravel 包开发中使用 Guzzle

[英]Use Guzzle in Laravel package development

Recently i am working on a package for Laravel.最近我正在为 Laravel 开发一个包。

I am at beginner level on Laravel Package Development.我在 Laravel 包开发方面处于初级水平。

I need to use Guzzle Client for an API request from this package.我需要使用Guzzle Client来处理来自这个包的 API 请求。

If i use use GuzzleHttp\\Client;如果我使用GuzzleHttp\\Client; in my controller, it's showing class not found.在我的控制器中,它显示找不到类。 (I know why. because it's outside of app folder and not autoloaded for this path) (我知道为什么。因为它在 app 文件夹之外并且没有为此路径自动加载)

Now, how can i use guzzle inside of my custom package controller.现在,我如何在我的自定义包控制器中使用 guzzle。

Here what i wanting:这是我想要的:

This is my controller method:这是我的控制器方法:

 public function packageList($vendor, $type)
    {
        $client = new Client();
        $result = $client->get('https://packagist.org/packages/list.json?vendor='.$vendor.'&type='.$type, [
            'form_params' => [
                'sample-form-data' => 'value'
            ]
        ]);

        dd($result);
    }

I attached classes of Guzzle我附加了 Guzzle 的课程

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

Getting Exception:获取异常:

Class 'GuzzleHttp\Client' not found

Found this answer on stack with the same question用同样的问题在堆栈上找到了这个答案

Open up your terminal at the root of your project and enter在项目的根目录打开终端并输入

composer require guzzlehttp/guzzle

It worked for the mailgun API.它适用于 mailgun API。 For some reason, the suggested method at the laravel's mail doc.出于某种原因,laravel 的邮件文档中建议的方法。 You may install this package to your project by adding the following line to your composer.json file您可以通过在 composer.json 文件中添加以下行来将此包安装到您的项目中

"guzzlehttp/guzzle": "~5.3|~6.0"

doesn't make the composer download the Guzzle source codes.不会让作曲家下载 Guzzle 源代码。 By the way, I didn't find out what |顺便说一句,我没有发现什么| means in determining the version.意味着确定版本。 This command just downloads the PSR code.此命令仅下载 PSR 代码。

In this moment, the solution may work.此时,解决方案可能会奏效。 However, be aware of compatibility issues.但是,请注意兼容性问题。 Because the command would install the latest stable version, not the suitable one.因为该命令会安装最新的稳定版本,而不是合适的版本。

answer by: shampoo回答者:洗发水

if this doesnt help, try reading the docs : http://docs.guzzlephp.org/en/stable/quickstart.html如果这没有帮助,请尝试阅读文档: http : //docs.guzzlephp.org/en/stable/quickstart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM