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 安装:
composer require guzzlehttp/guzzle
基本定制
让我们首先创建一个基本的自定义 guzzle http 客户端:
namespace appextensionsguzzle;
use guzzlehttpclient;
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 请求的快捷方法:
$client->get('/endpoint');
$client->post('/endpoint');
$client->put('/endpoint');
所有这些方法都使用了内部的通用请求方法。下面的截图取自guzzle客户端代码:
您可以重写请求方法来自定义应用程序对外部服务进行的 http 调用的管理。
namespace appextensionsguzzle;
use guzzlehttpclient;
use guzzlehttpexceptionrequestexception;
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 调用:
如果您是 inspector 新手,您可以按照本教程了解如何入门:
https://inspector.dev/laravel-real-time-performance-monitoring-using-inspector-part-1/
您还可以在回调中注入segment参数来与item交互或添加更多信息:
namespace appextensionsguzzle;
use guzzlehttpclient;
use guzzlehttpexceptionrequestexception;
use inspectormodelssegment;
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 客户端的行为进行任何更改,因此您可以创建自定义类的实例并照常使用它:
// 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 类创建一个绑定到容器中的单例。因此,每个请求此类的服务都会收到一个支持实时监控的自定义客户端实例。
use guzzlehttpclient;
use appextensionsguzzlecustomguzzleclient;
use illuminatecontractsfoundationapplication;
$this->app->singleton(client::class, function (application $app) {
return new customguzzleclient();
});
现在您可以尝试在 artisan command 中注入 guzzle http client 类并运行 http 调用以进行测试:
namespace appconsolecommands;
use illuminateconsolecommand;
use guzzlehttpclient;
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 调用是否在交易的时间线中可见:
php artisan try
督察新人?免费监控您的应用程序
inspector 是一款专为软件开发人员设计的代码执行监控工具。您无需在云基础设施或服务器中安装任何内容,只需安装 composer 包即可开始使用。
与其他复杂的一体化平台不同,inspector 超级简单,并且对 php 友好。您可以尝试我们的 laravel 或 symfony 包。
如果您正在寻找有效的自动化、深入的见解以及将警报和通知转发到消息传递环境的能力,请免费尝试 inspector。注册您的帐户。
或在网站上了解更多:https://inspector.dev