hakone/psr7-serializer 问题修复 & 功能扩展

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

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

hakone/psr7-serializer

最新稳定版本:0.0.2

Composer 安装命令:

composer require hakone/psr7-serializer

包简介

PSR-7 HTTP Message serializer

README 文档

README

Package version Build Status Downloads this Month

This package provides functionality to persist PSR-7 HTTP Responses based on and PSR-17.

This package is designed to serialize within the PSR-7 standard. The data does not have the pre-serialized class name, so the restored class depends only on the PSR-17 Factory object passed to unserializePsr7().

Install

# 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) is a package useful for auto-discovering classes provided by installed HTTP libraries.

If you do not already have HTTP factory implementations installed: consider adding one of nyholm/psr7, guzzlehttp/psr7, or laminas/laminas-diactoros to your project.

Usage

<?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);

If you don't have HTTPlug Discovery installed, you must explicitly pass an instance of HTTP Factory to unserialize HTTP messages:

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

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

    http://www.apache.org/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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-06-10