定制 amphp/http-client-psr7 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

amphp/http-client-psr7

最新稳定版本:v1.1.1

Composer 安装命令:

composer require amphp/http-client-psr7

包简介

PSR-7 adapter for Amp's HTTP client.

README 文档

README

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an PSR-7 adapter and PSR-18 client as a plugin for amphp/http-client.

Latest Release MIT License

Installation

This package can be installed as a Composer dependency.

composer require amphp/http-client-psr7

Requirements

  • PHP 8.1+

Usage

Create Amp\Http\Client\Psr7\PsrAdapter instance to convert client requests and responses between native Amp and PSR-7 formats. Adapter doesn't depend on any concrete PSR-7 implementation, so it requires PSR-17 factory interfaces to create PSR-7 requests and responses.

Use Amp\Http\Client\Psr7\PsrHttpClient as a drop-in implementation of a PSR-18 ClientInterface.

<?php

require 'vendor/autoload.php';

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Psr7\PsrAdapter;
use Amp\Http\Client\Psr7\PsrHttpClient;
use Laminas\Diactoros\RequestFactory;
use Laminas\Diactoros\ResponseFactory;

// Note the laminas/laminas-diactoros library is used only as an example.
// You can use any library providing an implementation of PSR-17.

// PSR-17 request factory
$psrRequestFactory = new RequestFactory();
// PSR-17 response factory
$psrResponseFactory = new ResponseFactory();

$psrAdapter = new PsrAdapter($psrRequestFactory, $psrResponseFactory);

$ampHttpClient = HttpClientBuilder::buildDefault();
$psrHttpClient = new PsrHttpClient($ampHttpClient, $psrAdapter);

$request = $psrRequestFactory->createRequest('GET', 'https://google.com');

// $response is a PSR-7 ResponseInterface instance.
$response = $psrHttpClient->sendRequest($request);

There are few incompatibilities between Amp and PSR-7 implementations that may require special handling:

  • PSR-7 requests contain only one protocol version, but Amp requests can contain several versions. In this case the adapter checks if the protocol version list contains a version that is the current PSR-7 implementation default, otherwise it throws an exception. You may also set the protocol version explicitly using the optional argument of the toPsrRequest() method.
  • Amp responses contain a reference to the Request instance, but PSR-7 responses don't; so you need to provide a request instance explicitly.

Examples

More extensive code examples reside in the examples directory.

Versioning

amphp/http-client-psr7 follows the semver semantic versioning specification like all other amphp packages.

Security

If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

  • 总下载量: 42.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 6
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固