Shortcut for making http api calls

1.5.1 2017-06-29 16:34 UTC

This package is auto-updated.

Last update: 2024-11-23 02:28:26 UTC


README

Build Status Latest Version Coverage Status

paulbunyannet/api Shortcut for making http calls to json endpoint

Installation

This project can be installed via [Composer]:

$ composer require paulbunyannet/api:^1.0

Request Methods:

Get

$getArgs = ['something' => ,'something_else']; 
$get = new Get('https://pathtoapi.com/get/?'.http_build_query($getArgs));
$retrieve = $get->retrieve();
var_dump($retrieve); // { "some_response_key" => "some_response_value" }
$getArgs = ['something' => ,'something_else']; 
$get = new Get('https://pathtoapi.com/get/?'.http_build_query($getArgs));
$retrieve = $get->retrieve();
var_dump($retrieve); // { "some_response_key" => "some_response_value" }

Post

$postArgs = ['something' => ,'something_else']; 
$post = new Post('https://pathtoapi.com/post');
$retrieve = $post->retrieve($postArgs);
var_dump($retrieve); // { "some_response_key" => "some_response_value" }

Put

$postArgs = ['something' => ,'something_else']; 
$put = new Put('https://pathtoapi.com/put');
$retrieve = $put->retrieve($postArgs);
var_dump($retrieve); // { "some_response_key" => "some_response_value" }

Delete

$delete = new Put('https://pathtoapi.com/delete/user/1');
$retrieve = $delete->retrieve();
var_dump($retrieve); // { "success" => "true" }

Headers

Header array can be passed into a new object:

$getArgs = ['something' => ,'something_else']; 
$get = new Get('https://pathtoapi.com/get/?'.http_build_query($getArgs));
// headers to pass with request
$get->setHeaders(['headerKey' => 'headerValue']);
$retrieve = $get->retrieve();
var_dump($retrieve); // { "some_response_key" => "some_response_value" }

The header "headerKey: headerValue" will then be passed on with the REST request

Authentication

Each request method can use payload authentication and then check that payload on the receiving end

// on the sender side:
$postArgs = ['something' => ,'something_else'];
$identity = 'my-user-name'; // used for looking up private key on the receiving side
$privateKey = "my-super-secret-key";
$post = new Post('https://pathtoapi.com/post');
$post->setPayload([Auth\AuthBootstrap::IDENTITY => $identity, Auth\AuthBootstrap::PRIVATEKEY => $privateKey]);
$retrieve = $post->retrieve($postArgs);

// then on the receiving side:
$lookUpPrivateKey = 'my-super-secret-key'; // this is where you would do a lookup for user's private key by the ideney key that was sent with the request
$receiver = new Auth\Receive($_POST['payload'], $lookUpPrivateKey);
$verifyHash = receiver->verifyHash($_POST); // will return true if payload hash sent is correct