2017 © Pedro Peláez
 

library httpclient

A simple PSR-7 client

image

sfn/httpclient

A simple PSR-7 client

  • Saturday, August 26, 2017
  • by sfn
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Sfn HttpClient

Latest Version MIT license PHP, (*1)

Just a simple and little PSR7 client. It is still a work-in-progress but, more or less, it works., (*2)

Table of Contents

Requirements

  • PHP 7.0 or higher
  • php-curl or allow_url_fopen set to true
  • A PSR7 implementation.

Sfn\HttpClient needs a PSR-7 implementation. It supports Zend Diactoros, Guzzle and Slim at the moment., (*3)

Of course you can write your own HTTP Factory implementation for any PSR-7 implementation, look here for PSR-17 Http Factory interfaces., (*4)

Installation

Install using Composer., (*5)

composer require sfn/httpclient

Zend Diactoros support

composer require http-interop/http-factory-diactoros

Guzzle support

composer require http-interop/http-factory-guzzle

Slim support

composer require http-interop/http-factory-slim

Usage

Create a client instance

With the ClientFactory::make() method you can create the correct instance of the client. If it finds curl installed, it creates a client with a curl backend, otherwise it create a client who send request via php's file_get_contents., (*6)

ClientFactory::make() accepts an associative array with the client configuration. You must specify at least your PSR-17 HTTP Factory implementation. Here you can find the complete list of parameters in you can set in the configuration array., (*7)

// Zend Diactoros
$config = [
    'requestfactory'  => new Http\Factory\Diactoros\RequestFactory,
    'responsefactory' => new Http\Factory\Diactoros\ResponseFactory,
    'urifactory'      => new Http\Factory\Diactoros\UriFactory,
];

// Guzzle
$config = [
    'requestfactory'  => new Http\Factory\Guzzle\RequestFactory,
    'responsefactory' => new Http\Factory\Guzzle\ResponseFactory,
    'urifactory'      => new Http\Factory\Guzzle\UriFactory,
];

// Slim
$config = [
    'requestfactory'  => new Http\Factory\Slim\RequestFactory,
    'responsefactory' => new Http\Factory\Slim\ResponseFactory,
    'urifactory'      => new Http\Factory\Slim\UriFactory,
];

$client = Sfn\HttpClient\ClientFactory::make($config);

Send a request

First of all, you must create a request with your preferred Psr\Http\Message\RequestInterface implementation. Then you simply call the send() method of the client., (*8)

$request = (new Zend\Diactoros\Request())
    ->withUri(new Zend\Diactoros\Uri('http://api.example.com/path'))
    ->withMethod('GET')
    ->withAddedHeader('Content-Type', 'application/json');

$response = $client->send($request); // Return a ResponseInterface

Helper methods for REST API

There are get(), post(), put(), delete() and patch() helper methods. You can pass a second parameter, with an array of options. Here you can find a complete list of request parameters., (*9)

// GET request
$response = $client->get('http://api.example.com/path');

// POST request
$response = $client->post(
    'http://api.example.com/path',
    ['body' => http_build_query(['foo' => 'bar'])]
);

Base URI

You can also specify a base uri in the client configuration., (*10)

$config = [
    'requestfactory'  => new Http\Factory\Diactoros\RequestFactory,
    'responsefactory' => new Http\Factory\Diactoros\ResponseFactory,
    'urifactory'      => new Http\Factory\Diactoros\UriFactory,
    'baseuri'         => 'http://api.example.com'
];
$client = Sfn\HttpClient\ClientFactory::make($config);

// GET request
$response = $client->get('path'); // GET http://api.example.com/path

To-Do

  • Cookies support
  • SSL authentication
  • Examples
  • Better documentation

License

MIT License, (*11)

The Versions

26/08 2017

dev-master

9999999-dev https://github.com/sfn/psr7-httpclient

A simple PSR-7 client

  Sources   Download

MIT

The Requires

 

psr-7 http client psr-17

26/08 2017

v0.1.1

0.1.1.0 https://github.com/sfn/psr7-httpclient

A simple PSR-7 client

  Sources   Download

MIT

The Requires

 

psr-7 http client psr-17

17/08 2017

v0.1.0

0.1.0.0 https://github.com/sfn/psr7-httpclient

A simple PSR-7 client

  Sources   Download

MIT

The Requires

 

psr-7 http client psr-17