pinkcrab/http
Composer 安装命令:
composer require pinkcrab/http
包简介
Wrapper around Nyholm\Psr7 library with a few helper methods and a basic emitter. For use in WordPress during ajax calls.
README 文档
README
Wrapper around Nyholm\Psr7 library with a few helper methods and a basic emitter. For use in WordPress during ajax calls.
Why?
Throughout a few of our modules we need to handle HTTP requests and responses. The WP_HTTP_* classes are great, but PS7 compliant libraries have a lot more to offer.
So this small module acts a wrapper for the Nyholm\Psr7 and Nyholm\Psr7Server libraries and gives a few helper methods. You can easily create and emit either Responses that extend WP_HTTP_RESPONSE or implements ResponseInterface
Examples
Creates a WP_HTTP_Response
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP; $http = new HTTP(); $response = $http->wp_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // OR $response = HTTP_Helper::wp_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // Emit to client $http->emit_response($response);
As both have the same signatures, you can interchange at will. Obviously the PS7 Response has more functionality to fine tune the response.
Creates a PS7 Response
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP; $http = new HTTP(); $response = $http->ps7_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // OR $response = HTTP_Helper::response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // Emit to client $http->emit_response($response);
Creates a PS7 Request
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $http = new HTTP(); $request = $http->psr7_request( 'GET', 'https://google.com' ); // OR $request = HTTP_Helper::request( 'GET', 'https://google.com' );
Get ServerRequest fromGlobals
Returns a populated instance of ServerRequestInterface.
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $server = (new HTTP())->request_from_globals(); // OR $server = HTTP_Helper::global_server_request();
Create Stream
The PSR7 HTTP objects work with streams for the body, you can wrap all scalars values which can cast to JSON in a stream.
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $stream = (new HTTP())->stream_from_scalar($data); // OR $stream = HTTP_Helper::stream_from_scalar($data);
The
(new HTTP())->create_stream_with_json()has been marked as deprecated since 0.2.3. use(new HTTP())->stream_from_scalar()in its place.
License
MIT License
http://www.opensource.org/licenses/mit-license.html
Tested Against
- PHP 8.0, 8.1, 8.2, 8.3 & 8.4
- WP 6.6, 6.7, 6.8 & 6.9
- MySQL 8.4
Change Log
- 1.2.0 - Drop PHP 7.x, require PHP 8.0+. Bump dev deps: phpstan 2.x, phpstan-wordpress 2.x, phpunit 8|9, WP 6.6-6.9 test matrix. Retire the
WP_6_2/WP_6_3/WP_6_4/WP_6_5workflows and addWP_6_6/WP_6_7/WP_6_8/WP_6_9(PHP 8.0-8.4,mysql:8.4). Standardise.scrutinizer.yml. Add curl-alternative and PSR12 file-header excludes tophpcs.xml. Suppress the WP 6.8wp_is_block_themeearly-call E_USER_NOTICE intests/wp-config.phpunder PHPUnit's notice-to-exception conversion. Fill in descriptions on every bare@param/@return/@throwsacrosssrc/. No public API changes. - 1.1.0 - Add nullable type hint to the reason phrase in the response and update dev dependencies
- 1.0.0 - Removed HTTP::create_stream_with_json()
- 0.2.6 - Readme changes
- 0.2.5 - Removed object type hint for param in emit_response
- 0.2.4 - Typo on scalar (all typed as scala)
- 0.2.3 - Added in
HTTP_Helperclass, patchedServerRequestfromGloablsto include the raw $_POST in its body. - 0.2.2 - Added the helper for wrapping data as json in Stream
- 0.2.1 - Removed die() from end of Emit calls and just returned back void. Die to happen at other end
- 0.2.0 - Moved from Guzzle being injected in constructor to using custom HTTP (pink crab). Plug move to composer format.
pinkcrab/http 适用场景与选型建议
pinkcrab/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.07k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pinkcrab/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pinkcrab/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-19