PHPackages                             codezero/courier - 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. codezero/courier

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

codezero/courier
================

Simple HTTP request interface with optional caching

2.1.1(11y ago)240.9k11MITPHPPHP &gt;=5.4.0

Since Aug 25Pushed 11y ago3 watchersCompare

[ Source](https://github.com/codezero-be/courier)[ Packagist](https://packagist.org/packages/codezero/courier)[ RSS](/packages/codezero-courier/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (1)

Courier - HTTP Requests Made Easy
=================================

[](#courier---http-requests-made-easy)

![GitHub release](https://camo.githubusercontent.com/460f8a36c76fbed5dce3ae4601a6ecce3fe8f7e106d5eb336ee5f43b4d45bb0e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f636f64657a65726f2d62652f636f75726965722e737667)![License](https://camo.githubusercontent.com/d11cd8bc1190eced2f1dcba0748c125e74cb84f74c2431484a7d9fac4739b21b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f64657a65726f2f636f75726965722e737667)[![Build Status](https://camo.githubusercontent.com/a9033eb4f428e56b5566e64e7c3889eeb150eadf1d2f6a06259f601becae72b9/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f64657a65726f2d62652f636f75726965722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/codezero-be/courier)[![Scrutinizer](https://camo.githubusercontent.com/5fd9dd62a270ce21eca431851f3dd6e73a9a7de17b9d7b79d64b879c0accf5d7/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f64657a65726f2d62652f636f75726965722e737667)](https://scrutinizer-ci.com/g/codezero-be/courier)[![Total Downloads](https://camo.githubusercontent.com/b94b966c329a77e3d18d3cb7d0e2f1f7aea5572dc9ea0958d9e199d408d78175/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64657a65726f2f636f75726965722e737667)](https://packagist.org/packages/codezero/courier)

This package offers an easy to use set of functions to send HTTP requests in PHP.

Features
--------

[](#features)

- Easy to use GET, POST, PUT, PATCH and DELETE functions (see [usage](#usage))
- Send optional data and headers with your requests
- Use optional basic authentication with your requests
- Optional caching of GET and POST responses
- [Laravel 5](http://www.laravel.com/ "Laravel") ServiceProvider included!

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

[](#installation)

Install this package through Composer:

```
"require": {
	"codezero/courier": "2.*"
}

```

Implementation
--------------

[](#implementation)

### Manual

[](#manual)

At this point there is only one Courier implementation: `CurlCourier`, which uses [codezero-be/curl](https://github.com/codezero-be/curl "codezero-be/curl") and the PHP cURL extension behind the scenes.

```
use CodeZero\Courier\CurlCourier;

$courier = new CurlCourier();

```

### Laravel 5

[](#laravel-5)

After installing, update your `config/app.php` file to include a reference to this package's service provider in the providers array:

```
'providers' => [
    'CodeZero\Courier\CourierServiceProvider'
]

```

Inject Courier
--------------

[](#inject-courier)

Inject the Courier instance into your own class:

```
use CodeZero\Courier\Courier; //=> The interface

class MyClass {

    private $courier;

    public function __construct(Courier $courier)
    {
        $this->courier = $courier;
    }
}

```

Laravel will then find the `Courier` class automatically if you registered the service provider. Or you can inject an instance manually: `$myClass = new MyClass($courier);`

Send a Request
--------------

[](#send-a-request)

### Configure a Request:

[](#configure-a-request)

```
$url = 'http://my.site/api';
$data = ['do' => 'something', 'with' => 'this']; //=> Optional
$headers = ['Some Header' => 'Some Value']; //=> Optional

```

### Enable Caching

[](#enable-caching)

Optional number of minutes to cache the request/response. Until it expires Courier will not actually hit the requested URL, but return a cached response! If you use Laravel then Laravel's [`Cache`](http://laravel.com/docs/5.0/cache) will be used. If not then [`phpFastCache`](https://github.com/khoaofgod/phpfastcache) is used.

```
$cacheMinutes = 30; //=> Default = 0 (no caching)

```

### Handle Exceptions

[](#handle-exceptions)

If the target server sends a HTTP error response &gt;= 400, a `CodeZero\Courier\Exceptions\HttpException` will be thrown.

Your class can also implement the `CodeZero\Courier\HttpExceptionHandler` interface and add a [`handleHttpException`](#response-issues) method. If you pass Courier a reference to your class, then no exception will be thrown. Instead, the `HttpException` will be passed to your [`handleHttpException`](#response-issues) method, so you can throw your own exception or return any value back to the original caller.

```
$handler = $this; //=> A reference to your class (optional)

```

### Send the Request: (one of the following)

[](#send-the-request-one-of-the-following)

```
$response = $this->courier->get($url, $data, $headers, $cacheMinutes, $handler);
$response = $this->courier->post($url, $data, $headers, $cacheMinutes, $handler);
$response = $this->courier->put($url, $data, $headers, $handler);
$response = $this->courier->patch($url, $data, $headers, $handler);
$response = $this->courier->delete($url, $data, $headers, $handler);

```

All of these methods will return an instance of the `CodeZero\Courier\Response` class.

Read the Response
-----------------

[](#read-the-response)

### Get the Response Body

[](#get-the-response-body)

```
$body = $response->getBody();

```

### Get Additional Request Info

[](#get-additional-request-info)

```
$httpCode = $response->getHttpCode(); //=> "200"
$httpMessage = $response->getHttpMessage(); //=> "OK"
$responseType = $response->getResponseType(); //=> "application/json"
$responseCharset = $response->getResponseCharset(); //=> "UTF-8"

```

### Convert the Response

[](#convert-the-response)

You can convert a JSON or serialized response to an associative array or to an array of generic PHP objects. If you are sending API requests, chances are high that you get `application/json` data in return. The Flickr API `php_serial` responses are of type `application/binary` and serializable.

To convert this data just run:

```
$array = $response->toArray();
echo $array[0]['some']['nested']['key'];

$objects = $response->toObjects();
echo $objects[0]->some->nested->key;

```

If for some reason the conversion fails, a `CodeZero\Courier\Exceptions\ResponseConversionException` will be thrown.

Caching
-------

[](#caching)

To enable caching, there are 2 conditions:

1. Courier needs to be instantiated with the `CodeZero\Courier\Cache\Cache` class and a valid implementation of the `CodeZero\Courier\Cache\CacheManager` has to be provided to this dependency. This is done automatically.
2. Caching minutes need to be greater than zero (default: 0)

### Cache Time

[](#cache-time)

Each request can have a different cache time. Just specify it in the method call:

```
$this->courier->get($url, $data, $headers, $cacheMinutes);

```

### Clear Cache at Runtime:

[](#clear-cache-at-runtime)

```
$this->courier->forgetCache();

```

Basic Authentication
--------------------

[](#basic-authentication)

If you require the use of basic authentication, you can enable this before sending the request:

```
$this->courier->setBasicAuthentication('username', 'password');

```

You can also undo this:

```
$this->courier->unsetBasicAuthentication();

```

Exceptions
----------

[](#exceptions)

#### Request Issues

[](#request-issues)

A `CodeZero\Courier\Exceptions\RequestException` will be thrown, if the request could not be executed. This might be the case if your request is not properly configured (unsupported protocol, etc.).

#### Response Issues

[](#response-issues)

A `CodeZero\Courier\Exceptions\HttpException` will be thrown, if there was a HTTP response error &gt;= 400. Unless you are using the [`handleHttpException`](#handle-exceptions) callback. In that case you must add this method to your class:

```
public function handleHttpException(HttpException $exception)
{
    $errorMessage = $exception->getMessage();
    $errorCode = $exception->getCode();
    $response = $exception->response();
    $responseBody = $response->getBody();

    // If it's JSON:
    $array = $response->toArray();

    // throw your exception
    // or return something
}

```

Testing
-------

[](#testing)

```
$ vendor/bin/phpspec run

```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

---

[![Analytics](https://camo.githubusercontent.com/85d25aea8d18cdffaf2cbb1a330cb676976c0ebea1c64cf7ca1eb2f51de1372d/68747470733a2f2f67612d626561636f6e2e61707073706f742e636f6d2f55412d35383837363031382d312f636f64657a65726f2d62652f636f7572696572)](https://github.com/igrigorik/ga-beacon)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~14 days

Recently: every ~3 days

Total

14

Last Release

4098d ago

Major Versions

1.1.2 → 2.0.02015-02-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e17b7a892452367dfb0e5c55bf04844a16bb781f356f50019332d4b9a476ec6?d=identicon)[codezero](/maintainers/codezero)

---

Top Contributors

[![ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?v=4)](https://github.com/ivanvermeyen "ivanvermeyen (38 commits)")

---

Tags

httprequestapilaravelrestcurlpost

### Embed Badge

![Health badge](/badges/codezero-courier/health.svg)

```
[![Health](https://phpackages.com/badges/codezero-courier/health.svg)](https://phpackages.com/packages/codezero-courier)
```

###  Alternatives

[pusher/pusher-http-laravel

\[DEPRECATED\] A Pusher bridge for Laravel

400509.0k3](/packages/pusher-pusher-http-laravel)[aplus/http-client

Aplus Framework HTTP Client Library

2161.6M1](/packages/aplus-http-client)[vinelab/http

An http library developed for the laravel framework. aliases itself as HttpClient

59300.2k11](/packages/vinelab-http)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[elementaryframework/water-pipe

URL routing framework and requests/responses handler for PHP

254.6k4](/packages/elementaryframework-water-pipe)[laragear/api-manager

Manage multiple REST servers to make requests in few lines and fluently.

161.8k](/packages/laragear-api-manager)

PHPackages © 2026

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