hakone / psr7-serializer
PSR-7 HTTP 消息序列化器
0.0.2
2023-06-11 00:21 UTC
Requires
- php: >=7.3
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- jangregor/phpstan-prophecy: ^1.0
- nyholm/psr7: ^1.8
- nyholm/psr7-server: ^1.0
- php-http/discovery: ^1.18
- phpspec/prophecy-phpunit: ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^9.6
- psr/http-client: ^1.0
- symplify/easy-coding-standard: ^11.2
This package is auto-updated.
Last update: 2024-09-11 02:55:43 UTC
README
此包提供了根据 PSR-7 和 PSR-17 持久化 PSR-7 HTTP 响应的功能。
此包设计用于在 PSR-7 标准内进行序列化。数据不包含预序列化的类名,因此恢复的类仅依赖于传递给 unserializePsr7()
的 PSR-17 工厂对象。
安装
# minimum requirements when your project already has any HTTP library installed
composer require hakone/psr7-serializer
# for auto-detecting HTTP libraries installed in your project
composer require hakone/psr7-serializer php-http/discovery
HTTPlug Discovery (php-http/discovery
) 是一个用于自动发现已安装 HTTP 库提供的类的包。
如果您尚未安装 HTTP 工厂实现,请考虑将 nyholm/psr7
、guzzlehttp/psr7
或 laminas/laminas-diactoros
添加到您的项目中。
用法
<?php use Http\Discovery\Psr17FactoryDiscovery; use Http\Discovery\Psr18ClientDiscovery; use function Hakone\Psr7Serializer\serializePsr7; use function Hakone\Psr7Serializer\unserializeResponse; // 1. Send HTTP Request $http_client = Psr18ClientDiscovery::find(); $response = $client->sendRequest( Psr17FactoryDiscovery::findRequestFactory() ->createRequest('GET', 'http://httpbin.org/get?foo=bar') ); // 2. Serialize and cache HTTP response $serialized_data = serializePsr7($serializer->serializeResponse($response)); file_put_contents(__DIR__ . '/cache.txt', $serialized_data); // 3. Load and unserialize HTTP response $loaded_serialized_data = file_get_contents(__DIR__ . '/cache.txt'); $unserialized_response = unserializePsr7($loaded_serialized_data);
如果您未安装 HTTPlug Discovery,您必须显式传递一个 HTTP 工厂实例以反序列化 HTTP 消息
use Nyholm\Psr7\Factory\Psr17Factory; $psr17Factory = new Psr17Factory(); $unserialized_response = unserializePsr7($loaded_serialized_data, $psr17Factory, $psr17Factory);
use Laminas\Diactoros\{ResponseFactory, StreamFactory}; $unserialized_response = unserializePsr7($loaded_serialized_data, new ResponseFactory(), new StreamFactory());
版权
Copyright 2023 USAMI Kenta <tadsan@zonu.me>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.