Skip to content

nicelocal/http-client-psr7

 
 

Repository files navigation

HTTP Client

Build Status CoverageStatus License

This package provides an PSR-7 adapter as a plugin for amphp/http-client.

Installation

This package can be installed as a Composer dependency.

composer require amphp/http-client-psr7

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.

<?php

require 'vendor/autoload.php';

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

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

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

// Convert PSR-7 request to Amp request
$psrRequest = $psrRequestFactory->createRequest('GET', 'https://google.com/');
$ampRequest = $psrAdapter->fromPsrRequest($psrRequest);

// Convert Amp request to PSR-7 request
$psrRequest = $psrAdapter->toPsrRequest($ampRequest);

// Convert PSR-7 response to Amp response
$psrResponse = $psrResponseFactory->createResponse();
$ampResponse = $psrAdapter->fromPsrResponse($psrResponse, $ampRequest);

// Convert Amp response to PSR-7 response
$psrResponse = $psrAdapter->toPsrResponse($ampResponse);

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 email me@kelunik.com instead of using the issue tracker.

License

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

About

PSR-7 adapter for amphp/http-client.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%