PHPackages                             johnshopkins/http-exchange - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. johnshopkins/http-exchange

ActiveLibrary[HTTP &amp; Networking](/categories/http)

johnshopkins/http-exchange
==========================

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

v1.0.3(2mo ago)121.3k↓33.3%MITPHPPHP &gt;=8.0

Since Jan 16Pushed 2mo agoCompare

[ Source](https://github.com/johnshopkins/http-exchange)[ Packagist](https://packagist.org/packages/johnshopkins/http-exchange)[ Docs](https://github.com/johnshopkins/http-exchange)[ RSS](/packages/johnshopkins-http-exchange/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (43)Used By (0)

HTTP Exchange
=============

[](#http-exchange)

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

Available adapters:

- [Guzzle 6](https://docs.guzzlephp.org/en/6.5/)
- [Guzzle 7](https://docs.guzzlephp.org/en/7.0/)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- Guzzle &gt;= 6.5

Installation
------------

[](#installation)

To install the library, you will need to use Composer in your project.

```
composer require johnshopkins/http-exchange
```

Basic usage
-----------

[](#basic-usage)

### Single request

[](#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

[](#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
--------------

[](#error-handling)

### Single request

[](#single-request-1)

If a request fails, a `HttpExchange\Exceptions\HTTP` exception is thrown. See the [exception methods documentation](#exception-methods) for more information.

```
$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

[](#batch-requests-1)

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.

```
$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:

```
$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
-------------

[](#documentation)

### Initialization

[](#initialization)

#### Guzzle 6

[](#guzzle-6)

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

#### Guzzle 7

[](#guzzle-7)

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

### Adapter methods

[](#adapter-methods)

#### **`batch(array $requests)`**

[](#batcharray-requests)

Send multiple requests concurrently.

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.

Arguments:

- `$requests`: An array of requests to make concurrently. Format: ```
    $requests = [[$method, $url, $request_options], ...];
    ```

    - `$method`: HTTP method
    - `$uri`: Request URI
    - `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`get(array $requests)`**

[](#getarray-requests)

Make a GET request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`post(array $requests)`**

[](#postarray-requests)

Make a POST request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`put(array $requests)`**

[](#putarray-requests)

Make a PUT request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`delete(array $requests)`**

[](#deletearray-requests)

Make a DELETE request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`patch(array $requests)`**

[](#patcharray-requests)

Make a PATCH request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`head(array $requests)`**

[](#headarray-requests)

Make a HEAD request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

#### **`options(array $requests)`**

[](#optionsarray-requests)

Make an OPTIONS request.

Returns: A `HttpExchange\Response` object. Throw a `HttpExchange\Exceptions\HTTP` exception object if the request fails.

Arguments:

- `$method`: HTTP method
- `$uri`: Request URI
- `$request_options`: Request options to pass to HTTP client ([Guzzle 6](https://docs.guzzlephp.org/en/6.5/request-options.html) or [Guzzle 7](https://docs.guzzlephp.org/en/7.0/request-options.html))

### Response methods

[](#response-methods)

#### **`getBody()`**

[](#getbody)

Get the body of the response.

Returns: `SimpleXMLElement` object if the response is XML. Object or array if the response is JSON.

#### **`getStatusCode()`**

[](#getstatuscode)

Get the HTTP status code of the response;

Returns: Integer

### Exception methods

[](#exception-methods)

All default methods available on PHP exceptions are available plus:

#### **`getStatusCode()`**

[](#getstatuscode-1)

Get the HTTP status code of the response;

Returns: Integer

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance88

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 93.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~120 days

Recently: every ~238 days

Total

41

Last Release

60d ago

Major Versions

v0.14 → v1.02023-09-21

PHP version history (3 changes)v0.1PHP &gt;=5.3.2

v0.14PHP &gt;=8.1

v1.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/11e1b915549459b87bab747538857fb810a047407ad1a0eeee929e23908d5490?d=identicon)[jenwachter](/maintainers/jenwachter)

![](https://www.gravatar.com/avatar/41cd9e63ba89b3c2eb72077856593d8fb2bd2579d74876bdcbb15bdec4dd2a1d?d=identicon)[ericconrad](/maintainers/ericconrad)

---

Top Contributors

[![jenwachter](https://avatars.githubusercontent.com/u/1202305?v=4)](https://github.com/jenwachter "jenwachter (99 commits)")[![jasonrhodes](https://avatars.githubusercontent.com/u/159370?v=4)](https://github.com/jasonrhodes "jasonrhodes (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/johnshopkins-http-exchange/health.svg)

```
[![Health](https://phpackages.com/badges/johnshopkins-http-exchange/health.svg)](https://phpackages.com/packages/johnshopkins-http-exchange)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
