chriha/rest-client

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple PHP REST client

Installs: 434

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 1

Open Issues: 1

Type:project

0.6.4 2017-03-06 15:31 UTC

This package is auto-updated.

Last update: 2023-12-12 23:10:23 UTC


README

Build Status

A simple REST client with PHPs cURL.

Install

composer require chriha/rest-client

Usage

Define your options

$options = [
    'url' => 'http://api.localhost/v1',
];

See \Chriha\Clients\Rest::getDefaultOptions() for all default options.

GET

$rest = new \Chriha\Clients\Rest( $options );
$rest->get( '/posts' );

POST

$post = [
    "title" => "lorem",
    "body"  => "lorem ipsum dolor set"
];

$rest = new \Chriha\Clients\Rest( $options );
$rest->post( '/posts', $post );

PUT / PATCH

$post = [
    "title" => "lorem"
];

$rest = new \Chriha\Clients\Rest( $options );
$rest->put( '/posts/1', $post );
$rest->patch( '/posts/1', $post );

DELETE

$rest = new \Chriha\Clients\Rest( $options );
$rest->delete( '/posts/1' );

Options

Allow self signed certificates

Recommended only in dev environment, so default is false

$options = [
    'allow_self_signed' => true,
];

Set additional cURL options

$options = [
    'curl_options' => [...],
];

OAuth 1.0 authentication

$options = [
    'authentication' => 'oauth1',
    'token'          => 'YOUR_API_TOKEN',
    'secret'         => 'YOUR_API_SECRET',
];

Using the CLI rest client

Make an alias like alias rest='vendors/bin/rest' for simpler usage of the client inside the project.

With the following command you can do a request via the rest client.

$ ./rest GET http://api.localhost.io/v1/posts "parameters=specified&as=simple&query=string" "Content-Type:application/json;Accept-Charset: utf-8"

If you want to use token and secret for your authentication, you can place them as JSON in the .rest file of your project root:

{
    "token": "YOUR_API_TOKEN",
    "secret": "YOUR_API_SECRET"
}

The output of the rest client will be shown as the following:

Request took 23.45ms
Response Code: 200
Response Body:
{
    "meta": "info",
    "data": [
        {
            "title": "lorem"
        }
    ]
}