Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

chriha/rest-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rest-client

Build Status

A simple REST client with PHPs cURL.

Install

composer require chriha/rest-client

Usage

Define your options

$options = [
    'url' => 'http:https://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:https://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"
        }
    ]
}