如何监控 Guzzle Http 客户端 – PHP 快速提示

2024-08-18 0 709

guzzle 是一款流行的 php HTTP 客户端,可以轻松发送 http 请求和创建 Web 服务库。最流行的 php 框架提供了内部 http client 服务,它们只是 guzzle http client 的定制实现:

  • Laravel http 客户端
  • Symfony http 客户端
  • laminas(以前的 zend framework)http 客户端

guzzle 被广泛使用有两个主要原因:

1) 定制化和灵活性

对于设计模式的爱好者来说,guzzle 是开放的扩展。意味着您可以通过扩展其核心组件(http client、request、response、milddeware 等)轻松地在 guzzle 中实现新功能。

2)对中间件的支持

guzzle 中间件系统允许开发人员在发送请求之前与请求进行交互,并在处理响应之前与响应进行交互。它可以启用日志记录、身份验证和错误处理等高级功能。

guzzle http 客户端简介

在本教程中,我将指导您完成创建自定义 guzzle http 客户端的过程,以便轻松监控应用程序针对外部服务发出的每个请求。

立即学习“PHP免费学习笔记(深入)”;

我还将向您展示如何将此实现注入到 ioc 容器(或服务容器)中,以使此实现在您的整个应用程序中可用。

我们将介绍基础知识、自定义选项,并提供真实的代码示例。

安装guzzle

确保您已安装 guzzle。如果没有,请使用 composer 安装:

1

composer require guzzlehttp/guzzle

基本定制

让我们首先创建一个基本的自定义 guzzle http 客户端:

1

2

3

4

5

6

7

8

9

10

11

12

namespace app\extensions\guzzle;

use guzzlehttp\client;

class customguzzleclient extends client

{

    public function __construct(array $config = [])

    {

        $config['HEADers']['custom-header'] = 'custom-value';

        parent::__construct($config);

    }

}

在此示例中,我们扩展了 guzzle http client 类并自定义构造函数,以向该客户端发出的所有请求添加自定义标头。

监控 guzzle http 请求

guzzle 提供了运行 http 请求的快捷方法:

1

2

3

$client->get('/endpoint');

$client->post('/endpoint');

$client->put('/endpoint');

所有这些方法都使用了内部的通用请求方法。下面的截图取自guzzle客户端代码

如何监控 Guzzle Http 客户端 – PHP 快速提示

您可以重写请求方法来自定义应用程序对外部服务进行的 http 调用的管理。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

namespace app\extensions\guzzle;

use guzzlehttp\client;

use guzzlehttp\exception\requestexception;

class customguzzleclient extends client

{

    public function request($method, $uri, array $options = [])

    {

        return inspector()->addsegment(function () use ($method, $uri, $options) {

            return parent::request($method, $uri, $options);

        }, "http", "{$method} {$uri}");

    }

}

在此示例中,我只是在每个请求的事务时间线中添加一个新项目。现在您可以在监控视图中看到 guzzle 进行的 API 调用:

如何监控 Guzzle Http 客户端 – PHP 快速提示

如果您是 inspector 新手,您可以按照本教程了解如何入门:

HTTPS://inspector.dev/laravel-real-time-perfORMance-monitoring-using-inspector-part-1/

您还可以在回调中注入segment参数来与item交互或添加更多信息:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

namespace app\extensions\guzzle;

use guzzlehttp\client;

use guzzlehttp\exception\requestexception;

use inspector\models\segment;

class customguzzleclient extends client

{

    public function request($method, $uri, array $options = [])

    {

        return inspector()->addsegment(function (segment $segment) use ($method, $uri, $options) {

            $response = parent::request($method, $uri, $options);

            $segment->label = "{$response->getstatuscode()} {$method} {$uri}";

            return $response;

        }, "http");

    }

}

使用自定义 http 客户端

现在,您可以在应用程序中使用自定义客户端。由于扩展不会对标准 guzzle http 客户端的行为进行任何更改,因此您可以创建自定义类的实例并照常使用它:

1

2

3

4

5

// create an instance of the custom client

$client = new customguzzleclient(['base_uri' => 'https://api.example.com']);

// make an api request. it will be automatically monitored by inspector.

$response = $client->get('/endpoint');

将 guzzle http client 绑定到容器中

在此示例中我将使用 laravel,但基本概念与本文开头提到的最流行的 php 框架相同。它们都与服务容器一起使用。

我们为 guzzle http client 类创建一个绑定到容器中的单例。因此,每个请求此类的服务都会收到一个支持实时监控的自定义客户端实例。

1

2

3

4

5

6

7

use guzzlehttp\client;

use app\extensions\guzzle\customguzzleclient;

use illuminate\contracts\foundation\application;

$this->app->singleton(client::class, function (application $app) {

    return new customguzzleclient();

});

现在您可以尝试在 artisan command 中注入 guzzle http client 类并运行 http 调用以进行测试:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

namespace app\console\commands;

use illuminate\console\command;

use guzzlehttp\client;

class trycommand extends command

{

    /**

     * the name and signature of the console command.

     *

     * @var string

     */

    protected $signature = 'try';

    /**

     * the console command descrIPtion.

     *

     * @var string

     */

    protected $description = 'test guzzle http client monitoring.';

    /**

     * inject the guzzle http client class into the constructor.

     * the customguzzleclient will be the concrete class.

     */

    public function __construct(protected client $client)

    {

        parent::__construct();

    }

    /**

     * handle the command execution.

     */

    public function handle()

    {

        $this->line($this->description);

        $this->line("concrete class: ".get_class($this->client));

        $this->client->get('https://google.com');

        return command::success;

    }

}

运行命令来验证 http 调用是否在交易的时间线中可见:

1

php artisan try

督察新人?免费监控您的应用程序

inspector 是一款专为软件开发人员设计的代码执行监控工具。您无需在云基础设施或服务器中安装任何内容,只需安装 composer 包即可开始使用。

与其他复杂的一体化平台不同,inspector 超级简单,并且对 php 友好。您可以尝试我们的 laravel 或 symfony 包。

如果您正在寻找有效的自动化、深入的见解以及将警报和通知转发到消息传递环境的能力,请免费尝试 inspector。注册您的帐户。

或在网站上了解更多:https://inspector.dev

如何监控 Guzzle Http 客户端 – PHP 快速提示

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 PHP 如何监控 Guzzle Http 客户端 – PHP 快速提示 https://www.xuezuoweb.com/13855.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用