paulbunyannet / api
Shortcut for making http api calls
Installs: 2 030
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
Requires (Dev)
- fzaninotto/faker: ^1.5
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^4.8
- satooshi/php-coveralls: ^1.0
README
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