pinkcrab / http
Nyholm\Psr7 库的包装器,包含一些辅助方法和基本的发射器。用于在 WordPress 的 AJAX 调用中。
1.0.0
2023-03-01 22:44 UTC
Requires
- php: >=7.2.0
- nyholm/psr7: ^1.3
- nyholm/psr7-server: ^1.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: *
- gin0115/wpunit-helpers: ~1
- php-stubs/wordpress-stubs: ^6.0 || ^5.9
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: ^6.1
- symfony/var-dumper: *
- szepeviktor/phpstan-wordpress: ^1.0
- vlucas/phpdotenv: ^5.4
- wp-coding-standards/wpcs: *
- wp-phpunit/wp-phpunit: ^6.1
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
This package is auto-updated.
Last update: 2024-09-07 09:39:31 UTC
README
Nyholm\Psr7 库的包装器,包含一些辅助方法和基本的发射器。用于在 WordPress 的 AJAX 调用中。
为什么?
在我们的几个模块中,我们需要处理 HTTP 请求和响应。WP_HTTP_* 类很棒,但 PS7 兼容的库提供更多功能。
因此,这个小型模块充当 Nyholm\Psr7 和 Nyholm\Psr7Server 库的包装器,并提供一些辅助方法。您可以轻松创建和发射扩展 WP_HTTP_RESPONSE 或实现 ResponseInterface 的响应。
示例
创建 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);
由于它们具有相同的签名,您可以根据需要随意交换。显然,PS7 响应有更多功能来微调响应。
创建 PS7 响应
<?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);
创建 PS7 请求
<?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' );
从全局变量获取 ServerRequest
返回一个填充的 ServerRequestInterface 实例。
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $server = (new HTTP())->request_from_globals(); // OR $server = HTTP_Helper::global_server_request();
创建流
PSR7 HTTP 对象使用流来处理正文,您可以包装所有可以转换为 JSON 的标量值。
<?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);
由于 0.2.3 版本的
(new HTTP())->create_stream_with_json()
已被标记为弃用,请使用(new HTTP())->stream_from_scalar()
替代。
许可证
MIT 许可证
https://open-source.org.cn/licenses/mit-license.html
变更日志
- 1.0.0 - 删除了 HTTP::create_stream_with_json()
- 0.2.6 - 修改了 Readme
- 0.2.5 - 从 emit_response 中的参数移除了对象类型提示
- 0.2.4 - scalar 上的错别字(所有类型都被标记为 scala)
- 0.2.3 - 添加了
HTTP_Helper
类,修补了ServerRequest
fromGloabls
以包括其正文中的原始 $_POST - 0.2.2 - 添加了将数据包装为 JSON 流的辅助方法
- 0.2.1 - 从 Emit 调用的末尾删除了 die() 并仅返回空值。Die 在另一端发生
- 0.2.0 - 从构造函数中注入 Guzzle 移动到使用自定义 HTTP(粉红蟹)。插件移动到 composer 格式。