2017 © Pedro Peláez
 

library rest

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

image

othercode/rest

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

  • Monday, October 16, 2017
  • by othercode
  • Repository
  • 3 Watchers
  • 7 Stars
  • 5,663 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

Rest Client

Tests Latest Stable Version License Total Downloads, (*1)

Rest client to make GET, POST, PUT, DELETE, PATCH, etc., (*2)

Installation

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

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

And run the following command:, (*4)

composer install

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:, (*5)

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

or, (*6)

$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:, (*7)

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

The rest client will throw a ConnectionException if there are any problem related to the connection., (*8)

NOTE: These errors are related to the session cURL, here is the complete list, (*9)

Methods

The available methods to work with are:, (*10)

get()

Perform a GET request., (*11)

Parameters Type Description
$url String Required. The URL to which the request is made
$data Array Optional. Associative array of data parameters

Return: Response object, (*12)

Perform a HEAD request., (*13)

Parameters Type Description
$url String Required. The URL to which the request is made

Return: Response object (no body), (*14)

post()

Perform a POST request., (*15)

Parameters Type Description
$url String Required. The URL to which the request is made
$data Array Optional. Associative array of data parameters

Return: Response object, (*16)

delete()

Perform a DELETE request., (*17)

Parameters Type Description
$url String Required. The URL to which the request is made
$data Array Optional. Associative array of data parameters

Return: Response object, (*18)

put()

Perform a PUT request., (*19)

Parameters Type Description
$url String Required. The URL to which the request is made
$data Array Optional. Associative array of data parameters

Return: Response object, (*20)

patch()

Perform a PATCH request., (*21)

Parameters Type Description
$url String Required. The URL to which the request is made
$data Array Optional. Associative array of data parameters

Return: Response object, (*22)

getMetadata()

Return the metadata of the request., (*23)

Return: Array, (*24)

getError()

Return the last known error., (*25)

Return: Error object, (*26)

getPayloads()

Return an array with the Response and Request objects., (*27)

Return: Array, (*28)

setDecoder()

Set a new Decoder., (*29)

Parameters Type Description
$name String Required. The unique name of the decoder.
$decoder String Optional. The class name with namespace of the new decoder.

Return: Rest object, (*30)

setEncoder()

Set a new Encoder., (*31)

Parameters Type Description
$name String Required. The unique name of the encoder.
$encoder String Optional. The class name with namespace of the new encoder.

Return: Rest object, (*32)

setModule()

Set a new Module., (*33)

Parameters Type Description
$name String Required. The unique name of the module.
$module String Required. The class name with namespace of the new module.
$hook String Optional. The hook name (after/before) that will trigger the module, 'after' by default.

Return: Rest object, (*34)

unsetModule()

Unregister a module., (*35)

Parameters Type Description
$moduleName String Required. The unique name of the decoder.
$hook String Optional. The hook name (after/before) from where delete the module.

Return: Rest object, (*36)

addHeader()

Add a new header., (*37)

Parameters Type Description
$header String Required. The unique name of the header.
$value String Requires. The value of the header.

Return: Rest object, (*38)

addHeaders()

Add an array of headers., (*39)

Parameters Type Description
$headers String Required. An array of headers.

Return: Rest object, (*40)

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

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

is the same as, (*42)

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

removeHeader()

Remove a header offset., (*43)

Parameters Type Description
$header String Required. The unique name of the header.

Return: Rest object, (*44)

removeHeaders()

Remove an array of headers., (*45)

Parameters Type Description
$headers String Required. An array of headers to remove.

Return: Rest object, (*46)

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:, (*47)

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

IMPORTANT: Any module MUST extends BaseModule, (*48)

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

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., (*50)

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

For "before" modules you can use all the properties of the Request object., (*51)

  • method
  • url
  • headers
  • body

For "after" modules you can use all the properties of the Response object., (*52)

  • 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., (*53)

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:, (*54)

$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., (*55)

Custom Decoders

To create a new decoder we only have to use this template:, (*56)

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., (*57)

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., (*58)

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

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"
}

The Versions

16/10 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • ext-curl *
  • php >=5.5

 

The Development Requires

curl rest http requests

11/10 2017

dev-develop

dev-develop

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

  Sources   Download

MIT

The Requires

 

The Development Requires

curl rest http requests

11/10 2017

v1.4.0

1.4.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-curl *

 

The Development Requires

curl rest http requests

10/10 2017

v1.3.2

1.3.2.0

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-curl *

 

The Development Requires

curl rest http requests

13/03 2017

v1.3.1

1.3.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-curl *

 

The Development Requires

curl rest http requests

06/03 2017

v1.3.0

1.3.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-curl *

 

The Development Requires

curl rest http requests

20/07 2016

v1.2.0

1.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-curl *

 

The Development Requires

curl rest http requests

23/04 2016

v1.0.1

1.0.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-curl *

 

The Development Requires

curl rest http requests

31/03 2016

v1.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • ext-curl *

 

The Development Requires

curl rest http requests