2017 © Pedro Peláez
 

library http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

image

johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  • Tuesday, January 23, 2018
  • by jenwachter
  • Repository
  • 0 Watchers
  • 0 Stars
  • 9,132 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 36 Versions
  • 2 % Grown

The README.md

HTTP Exchange

A collection of PHP HTTP client adapters to make swapping out HTTP client dependencies quick and easy., (*1)

Available adapters:, (*2)

Requirements

  • PHP >= 8.0
  • Guzzle >= 6.5

Installation

To install the library, you will need to use Composer in your project., (*3)

composer require johnshopkins/http-exchange

Basic usage

Single request

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$response = $http->get('http://httpbin.org/get');
$body = $response->getBody();
echo $body->url;

// prints: http://httpbin.org/get

Batch requests

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['post', 'http://httpbin.org/post']
]);

foreach ($responses as $response) {
  $body = $response->getBody();
  echo $body->url . "\n";
}

// prints:
// http://httpbin.org/get
// http://httpbin.org/post

Error handling

Single request

If a request fails, a HttpExchange\Exceptions\HTTP exception is thrown. See the exception methods documentation for more information., (*4)

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

try {
  $response = $http->get('http://httpbin.org/status/503');
  $body = $response->getBody();
} catch (\Exception $e) {
  echo $->getCode() . ': ' . $e->getMessage();
}

// prints:  503: Server error: `GET http://httpbin.org/status/503` resulted in a `503 SERVICE UNAVAILABLE` response

Batch requests

Instead of throwing a HttpExchange\Exceptions\HTTP when any one of the requests in a batch request fails, the exception is instead returned. This allows your application to gracefully handle failed requests, while processing successful ones., (*5)

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['get', 'http://httpbin.org/status/503']
]);

foreach ($responses as $response) {
  if ($response->getStatusCode() === 200) {
    $body = $response->getBody();
    echo $body->url . "\n";
  } else {
    echo $body->url . "This request failed :(\n";
  }
}

// prints:
// http://httpbin.org/get
// This request failed :(

Alternatively, you can check which kind of object was returned from each request (HttpExchange\Response or HttpExchange\Exceptions\HTTP) and proceed accordingly:, (*6)

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['get', 'http://httpbin.org/status/503']
]);

foreach ($responses as $response) {
  if ($response instanceof HttpExchange\Response) {
    $body = $response->getBody();
    echo $body->url . "\n";
  } else {
    echo $body->url . "This request failed :(\n";
  }
}

// prints:
// http://httpbin.org/get
// This request failed :(

Documentation

Initialization

Guzzle 6

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle6($client);

Guzzle 7

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

Adapter methods

batch(array $requests)

Send multiple requests concurrently., (*7)

Returns: array containing the result of each request. A HttpExchange\Response object indicates a successful request while a HttpExchange\Exceptions\HTTP exception object indicates a failed request., (*8)

Arguments:, (*9)

  • $requests: An array of requests to make concurrently. Format: php $requests = [[$method, $url, $request_options], ...];
    • $method: HTTP method
    • $uri: Request URI
    • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

get(array $requests)

Make a GET request., (*10)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*11)

Arguments:, (*12)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

post(array $requests)

Make a POST request., (*13)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*14)

Arguments:, (*15)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

put(array $requests)

Make a PUT request., (*16)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*17)

Arguments:, (*18)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

delete(array $requests)

Make a DELETE request., (*19)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*20)

Arguments:, (*21)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

patch(array $requests)

Make a PATCH request., (*22)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*23)

Arguments:, (*24)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

head(array $requests)

Make a HEAD request., (*25)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*26)

Arguments:, (*27)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

options(array $requests)

Make an OPTIONS request., (*28)

Returns: A HttpExchange\Response object. Throw a HttpExchange\Exceptions\HTTP exception object if the request fails., (*29)

Arguments:, (*30)

  • $method: HTTP method
  • $uri: Request URI
  • $request_options: Request options to pass to HTTP client (Guzzle 6 or Guzzle 7)

Response methods

getBody()

Get the body of the response., (*31)

Returns: SimpleXMLElement object if the response is XML. Object or array if the response is JSON., (*32)

getStatusCode()

Get the HTTP status code of the response;, (*33)

Returns: Integer, (*34)

Exception methods

All default methods available on PHP exceptions are available plus:, (*35)

getStatusCode()

Get the HTTP status code of the response;, (*36)

Returns: Integer, (*37)

The Versions

23/01 2018

dev-master

9999999-dev http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

23/01 2018

v0.12.3

0.12.3.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

19/01 2018

v0.12.2

0.12.2.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

19/01 2018

v0.12.1

0.12.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

19/01 2018

v0.12

0.12.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

01/12 2017

v0.11

0.11.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

30/11 2017

dev-develop

dev-develop http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

api http wrapper interfaces adapters

01/11 2017

v0.10.5

0.10.5.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

30/10 2017

v0.10.4

0.10.4.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

30/10 2017

v0.10.3

0.10.3.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

26/10 2017

v0.10.2

0.10.2.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

26/09 2017

v0.10.1

0.10.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

26/09 2017

v0.10

0.10.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

08/09 2017

v0.9

0.9.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

16/06 2017

v0.8

0.8.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

10/05 2017

v0.7.4

0.7.4.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

09/05 2017

v0.7.3

0.7.3.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

09/05 2017

v0.7.2

0.7.2.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

09/05 2017

v0.7.1

0.7.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

09/05 2017

v0.7

0.7.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

08/05 2017

v0.6.1

0.6.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

05/05 2017

v0.6

0.6.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

03/05 2017

v0.5.1

0.5.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

03/05 2017

v0.5

0.5.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

01/05 2017

v0.4.2

0.4.2.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

27/03 2017

v0.4.1

0.4.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

09/01 2017

v0.4

0.4.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

10/08 2016

v0.3.6

0.3.6.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

25/02 2016

v0.3.5

0.3.5.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

23/09 2014

v0.3.4

0.3.4.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

09/01 2014

v0.3.3

0.3.3.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

01/03 2013

v0.3.2

0.3.2.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

06/02 2013

v0.3.1

0.3.1.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

05/02 2013

v0.3

0.3.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

01/02 2013

v0.2

0.2.0.0 http://github.com/johnshopkins/http-exchange

A collection of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters

16/01 2013

v0.1

0.1.0.0 http://github.com/johnshopkins/http-exchange

A library of PHP interfaces and adapters to make swapping out HTTP library dependencies quick and easy.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

api http wrapper interfaces adapters