saeedhosan/guzzle-handler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saeedhosan/guzzle-handler

Composer 安装命令:

composer require saeedhosan/guzzle-handler

包简介

The package adds middlewire to laravel event for http client

README 文档

README

A Guzzle handler and middleware that dispatches Laravel HTTP Client events.

Introduction

This package provides a Guzzle handler that automatically dispatches standard Laravel HTTP client events (RequestSending, ResponseReceived, ConnectionFailed) when making requests. This allows you to leverage Laravel's built-in event listeners, logging, and monitoring even when using a custom Guzzle client instead of the Http facade.

Requirements

  • PHP ^8.2
  • Guzzle ^7.9
  • Laravel Framework ^11.0, ^12.0, or ^13.0

Installation

Install the package via composer:

composer require saeedhosan/guzzle-handler

Usages

Using the HttpClientEvent Handler

The HttpClientEvent handler can be used to wrap an existing Guzzle handler (like CurlHandler or MockHandler) to ensure Laravel events are dispatched.

use GuzzleHttp\Client;
use SaeedHosan\GuzzleHandler\Handlers\HttpClientEvent;

$handler = HttpClientEvent::make()
    ->withHandler(new \GuzzleHttp\Handler\CurlHandler());

$client = new Client([
    'handler' => $handler,
]);

// This request will now dispatch RequestSending and ResponseReceived events
$client->get('https://example.com');

Using mapFailure Middleware

You can also use the Middleware::mapFailure to handle request rejections with a custom callback:

use GuzzleHttp\HandlerStack;
use SaeedHosan\GuzzleHandler\Middleware;

$stack = HandlerStack::create();
$stack->push(Middleware::mapFailure(function ($reason) {
    // Handle the failure (e.g., log it or increment a counter)
}));

$client = new Client(['handler' => $stack]);

Testing

Run the tests using the following command:

composer test

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-26