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.4(2mo ago)121.8k↓80.8%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 yesterday

READMEChangelog (5)Dependencies (6)Versions (44)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 97% of packages

Maintenance85

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 93.5% 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 ~118 days

Recently: every ~234 days

Total

42

Last Release

78d 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 (100 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

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k114](/packages/httpsoft-http-message)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.4M91](/packages/mezzio-mezzio-router)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)

PHPackages © 2026

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