PHPackages                             othercode/rest - 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. othercode/rest

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

othercode/rest
==============

Rest client to make GET, POST, PUT, DELETE, PATCH, etc calls.

v1.5.0(2y ago)1045.2k↓48.1%42MITPHPPHP &gt;=7.4.33CI failing

Since Mar 31Pushed 2y ago3 watchersCompare

[ Source](https://github.com/othercodes/rest)[ Packagist](https://packagist.org/packages/othercode/rest)[ RSS](/packages/othercode-rest/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (10)Used By (2)

Rest Client
===========

[](#rest-client)

[![Tests](https://github.com/othercodes/rest/actions/workflows/test.yml/badge.svg)](https://github.com/othercodes/rest/actions/workflows/test.yml) [![Latest Stable Version](https://camo.githubusercontent.com/6da1d70ab81bfd1aa1684600074c33e88ddc05668d3e9bbe3f9aa70660c724cd/68747470733a2f2f706f7365722e707567782e6f72672f6f74686572636f64652f726573742f762f737461626c65)](https://packagist.org/packages/othercode/rest) [![License](https://camo.githubusercontent.com/cba452adbf1d6b50635d5dc7916cec2da3f124fb5641a36f1241c5efb9e337e8/68747470733a2f2f706f7365722e707567782e6f72672f6f74686572636f64652f726573742f6c6963656e7365)](https://packagist.org/packages/othercode/rest)[![Total Downloads](https://camo.githubusercontent.com/a6e681fb4ed04e66e52d8edef8fcaf82c44b6fe1971f06c1009417400abb7529/68747470733a2f2f706f7365722e707567782e6f72672f6f74686572636f64652f726573742f646f776e6c6f616473)](https://packagist.org/packages/othercode/rest)

[Rest client](http://othercode.es/packages/rest-client.html) to make GET, POST, PUT, DELETE, PATCH, etc.

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

[](#installation)

To install the package we only have to add the dependency to ***composer.json*** file:

```
{
  "require": {
    "othercode/rest": "*"
  }
}
```

And run the following command:

```
composer install
```

Usage
-----

[](#usage)

To use the Rest we only have to instantiate it and configure the params we want. We can establish the configuration accessing to the `->configuration->configure_property`, for example to configure the url of the call we only have to set the `->configuration->url parameter` like we can see as follows:

```
$api = new \OtherCode\Rest\Rest();
$api->configuration->url = "https://jsonplaceholder.typicode.com/";
```

or

```
$api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
    'url' => 'https://jsonplaceholder.typicode.com/',
)));
```

After this we have to set the type of call and the parameters that we wil use, in this case we are going to perform a **GET** request to the **"posts/1"** endpoint:

```
$response = $api->get("posts/1");
```

The rest client will throw a `ConnectionException` if there are any problem related to the connection.

**NOTE: These errors are related to the session cURL, here is the [complete list](https://curl.haxx.se/libcurl/c/libcurl-errors.html)**

Methods
-------

[](#methods)

The available methods to work with are:

#### `get()`

[](#get)

Perform a GET request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made`$data`ArrayOptional. Associative array of data parameters**Return**: Response object

#### `head()`

[](#head)

Perform a HEAD request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made**Return**: Response object (no body)

#### `post()`

[](#post)

Perform a POST request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made`$data`ArrayOptional. Associative array of data parameters**Return**: Response object

#### `delete()`

[](#delete)

Perform a DELETE request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made`$data`ArrayOptional. Associative array of data parameters**Return**: Response object

#### `put()`

[](#put)

Perform a PUT request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made`$data`ArrayOptional. Associative array of data parameters**Return**: Response object

#### `patch()`

[](#patch)

Perform a PATCH request.

ParametersTypeDescription`$url`StringRequired. The URL to which the request is made`$data`ArrayOptional. Associative array of data parameters**Return**: Response object

#### `getMetadata()`

[](#getmetadata)

Return the metadata of the request.

**Return**: Array

#### `getError()`

[](#geterror)

Return the last known error.

**Return**: `Error` object

#### `getPayloads()`

[](#getpayloads)

Return an array with the `Response` and `Request` objects.

**Return**: Array

#### `setDecoder()`

[](#setdecoder)

Set a new Decoder.

ParametersTypeDescription`$name`StringRequired. The unique name of the decoder.`$decoder`StringOptional. The class name with namespace of the new decoder.**Return**: Rest object

#### `setEncoder()`

[](#setencoder)

Set a new Encoder.

ParametersTypeDescription`$name`StringRequired. The unique name of the encoder.`$encoder`StringOptional. The class name with namespace of the new encoder.**Return**: Rest object

#### `setModule()`

[](#setmodule)

Set a new Module.

ParametersTypeDescription`$name`StringRequired. The unique name of the module.`$module`StringRequired. The class name with namespace of the new module.`$hook`StringOptional. The hook name (after/before) that will trigger the module, 'after' by default.**Return**: Rest object

#### `unsetModule()`

[](#unsetmodule)

Unregister a module.

ParametersTypeDescription`$moduleName`StringRequired. The unique name of the decoder.`$hook`StringOptional. The hook name (after/before) from where delete the module.**Return**: Rest object

#### `addHeader()`

[](#addheader)

Add a new header.

ParametersTypeDescription`$header`StringRequired. The unique name of the header.`$value`StringRequires. The value of the header.**Return**: Rest object

#### `addHeaders()`

[](#addheaders)

Add an array of headers.

ParametersTypeDescription`$headers`StringRequired. An array of headers.**Return**: Rest object

**NOTE: We can use the `addHeader()` and `addHeaders()` methods with the `Rest` instance or with the `configuration`object**

```
$api->addHeader('some_header','some_value');
$api->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));
```

is the same as

```
$api->configuration->addHeader('some_header','some_value');
$api->configuration->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));
```

#### `removeHeader()`

[](#removeheader)

Remove a header offset.

ParametersTypeDescription`$header`StringRequired. The unique name of the header.**Return**: Rest object

#### `removeHeaders()`

[](#removeheaders)

Remove an array of headers.

ParametersTypeDescription`$headers`StringRequired. An array of headers to remove.**Return**: Rest object

Modules
-------

[](#modules)

This package allow you to create modules to perform task before and after the request. To create a new module we only have to use this template:

```
class CustomModule extends BaseModule
{
    public function run()
    {
        // do something
    }
}
```

**IMPORTANT: Any module MUST extends BaseModule**

The only method that is mandatory is `->run()`, this method execute your custom code of the module.

Once we have our module we can register it with the `->setModule()` method. This method needs three parameters, the first one is the name of the module, the second one is the complete namespace of the module, and the third one is the hook name for our module, it can be "before" and "after" depends on when we want to launch our module.

```
$api->setModule('module_name','Module\Complete\Namespace','after');
```

For "before" modules you can use all the properties of the Request object.

- `method`
- `url`
- `headers`
- `body`

For "after" modules you can use all the properties of the Response object.

- `code`
- `content_type`
- `charset`
- `body`
- `headers`
- `error`
- `metadata`

All modules are executed in the order that we register them into the Rest client, this also affect to Decoders and Encoders.

Decoders
--------

[](#decoders)

A decoder is a kind of module that allows you to automatically decode de response in xml or json, to use them we only have to set the decoder we want with the `->setDecoder()` method:

```
$api->setDecoder("json");
```

The default allowed values for this method are: ***json***, ***xml*** and ***xmlrpc***. All the decoders are always executed in the "after" hook.

### Custom Decoders

[](#custom-decoders)

To create a new decoder we only have to use this template:

```
class CustomDecoder extends BaseDecoder
{
	protected $contentType = 'application/json';

	protected function decode()
	{
		// decode $this->body
	}
}
```

Like in modules, we have the Response object available to work. The $contentType property is the content-type that will trigger the decoder, in the example above all responses with content-type "application/json" will trigger this decoder.

### Quick Calls

[](#quick-calls)

We can do quick calls using the `\OtherCode\Rest\Payloads\Request::call()` method. This static method returns a Rest instance, so we can use all the methods from it.

```
$response = \OtherCode\Rest\Payloads\Request::call('https://jsonplaceholder.typicode.com')
    ->setDecoder('json')
    ->get('/posts/1');

```

Complete Example
----------------

[](#complete-example)

```
require_once '../autoload.php';

try {

    $api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
        'url' => 'https://jsonplaceholder.typicode.com/',
        'httpheader' => array(
            'some_header' => 'some_value',
        )
    )));
    $api->setDecoder("json");

    $response = $api->get("posts/1");
    var_dump($response);

} catch (\Exception $e) {
    print "> " . $e->getMessage() . "\n"
}
```

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 90% 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 ~392 days

Recently: every ~602 days

Total

8

Last Release

951d ago

PHP version history (3 changes)v1.0PHP &gt;=5.3.3

v1.3.2PHP &gt;=5.5

v1.5.0PHP &gt;=7.4.33

### Community

Maintainers

![](https://www.gravatar.com/avatar/7958790dcf5cd51ebbadc52fc7bada326fb3edd4481decf1006e6cfed8e46c43?d=identicon)[usantisteban](/maintainers/usantisteban)

---

Top Contributors

[![othercodes](https://avatars.githubusercontent.com/u/4815856?v=4)](https://github.com/othercodes "othercodes (27 commits)")[![christopheg](https://avatars.githubusercontent.com/u/199087?v=4)](https://github.com/christopheg "christopheg (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![ItsReddi](https://avatars.githubusercontent.com/u/3147888?v=4)](https://github.com/ItsReddi "ItsReddi (1 commits)")

---

Tags

curldecoderencoderhttphttp-clientresthttprestcurlrequests

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/othercode-rest/health.svg)

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

###  Alternatives

[nategood/httpful

A Readable, Chainable, REST friendly, PHP HTTP Client

1.8k17.2M267](/packages/nategood-httpful)[voku/httpful

A Readable, Chainable, REST friendly, PHP HTTP Client

16183.9k1](/packages/voku-httpful)

PHPackages © 2026

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