webignition/http-history-container
Composer 安装命令:
composer require webignition/http-history-container
包简介
A container for Guzzle history middleware
README 文档
README
Helping you more easily test what your Guzzle HTTP client has been up to.
A container for Guzzle history middleware offering smashingly-nice access to:
- collections of HTTP transactions (requests plus responses)
- requests made
- responses received
- request URLs
Oh also logging. Really useful when your Guzzle client under test is not executing within the same thread as your tests.
Usage
Basic Usage
<?php use GuzzleHttp\Client as HttpClient; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use webignition\HttpHistoryContainer\Container; $historyContainer = new Container(); $historyHandler = Middleware::history($historyContainer); $handlerStack = HandlerStack::create($historyHandler); $httpClient = new HttpClient(['handler' => $handlerStack]); /// ... things happen ... $historyContainer->getTransactions(); // an array of HttpTransaction $historyContainer->getRequests(); // a Collection\RequestCollection $historyContainer->getResponses(); // a Collection\ResponseCollection foreach ($historyContainer as $transaction) { // $transaction is a HttpTransaction $request = $transaction->getRequest(); // $request is a RequestInterface $response = $transaction->getResponse(); // $response is a ResponseInterface }
Details on Requests Sent
<?php use webignition\HttpHistoryContainer\Container; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; $historyContainer = new Container(); // ... create and use Guzzle client ... $transactions = $historyContainer->getTransactions(); // $transactions is a Collection\TransactionCollection $requests = $transactions->getRequests(); // $requests is a Collection\RequestCollection // RequestCollection is iterable foreach ($requests as $request) { // $request is a RequestInterface } $urls = $requests->getUrls(); // $urls is a Collection\UrlCollection // UrlCollection is iterable foreach ($urls as $url) { // $url is a UriInterface } foreach ($urls->getAsStrings() as $urlString) { // convenient access to requested URLs as strings }
Details on Responses Received
<?php use webignition\HttpHistoryContainer\Container; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; $historyContainer = new Container(); // ... create and use Guzzle client ... $transactions = $historyContainer->getTransactions(); // $transactions is a Collection\TransactionCollection $responses = $transactions->getResponses(); // $responses is a Collection\ResponseCollection // ResponseCollection is iterable foreach ($responses as $response) { // $response is a ResponseInterface }
Logging!
You may not be able to access your Guzzle history if the client under test is executing in a different thread from your tests.
A LoggableContainer allows you to record transactions as they happen to whatever Psr\Log\LoggerInterface
instance you like.
The logging container wraps each HttpTransaction in a LoggableTransaction object which is
serialized to JSON and output as a debug message.
LoggableTransactionFactory::createFromJson() lets you (in a somewhat slightly lossy manner) re-create
transactions object from logged messages.`
Logging Example
<?php use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; use webignition\HttpHistoryContainer\LoggableContainer; use webignition\HttpHistoryContainer\Factory\LoggableTransactionFactory; // Create the PSR logger $path = '/path/to/log'; $stream = fopen($path, 'w+'); $logger = new Logger(''); $logHandler = new StreamHandler($stream); $logHandler ->setFormatter(new LineFormatter('%message%' . "\n")); $logger->pushHandler($logHandler); // Create LoggableContainer $container = new LoggableContainer($logger); // ... create and use Guzzle client ... $logContent = file_get_contents($path); $logLines = array_filter(explode("\n", $logContent)); $loggedTransactions = []; foreach ($logLines as $logLine) { $loggedTransactions[] = LoggableTransactionFactory::createFromJson($logLine); } // $loggedTransactions is now an array of LoggableTransaction foreach ($loggedTransactions as $loggedTransaction) { $transaction = $loggedTransaction->getTransaction(); // $transaction is a HttpTransaction $request = $transaction->getRequest(); // $request is a RequestInterface $response = $transaction->getResponse(); // $response is a ResponseInterface }
webignition/http-history-container 适用场景与选型建议
webignition/http-history-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90.93k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「Guzzle」 「middleware」 「history」 「psr-7」 「guzzlehttp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webignition/http-history-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webignition/http-history-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webignition/http-history-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A fast and intuitive dependency injection container.
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
Simplifies working with holaluz API endpoints
Wrapper for Slack Web API
统计信息
- 总下载量: 90.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 31
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-02