pbmengine / php-restclient
Generic PHP client to access REST APIs
0.9.7
2019-10-07 07:50 UTC
Requires
- php: ^7.2.0
- guzzlehttp/guzzle: ^6.3
- illuminate/support: 5.7.*|5.8.*|6.0.*
- mockery/mockery: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.0
README
This publication describes a generic api rest client suitable for all PHP packages.
Installation
You can install the package via composer:
composer require pbmengine/php-restclient
Basic Usage
use Pbmengine\Restclient\HttpClient; $client = new HttpClient; // or $client = new HttpClient(\GuzzleHttp\Client, 'https://example.com/v1', ['timeout' => 30]); $response = $client->get('users'); $response = $client->jsonPayload(['id' : 1])->post('users');
Client Methods
$client->baseUrl('https://example.com/v1');
$client->options(['http_errors' => false, 'timeout' => 30]);
$client->option('http_errors', false);
$client->authorizationBearer('token');
$client->authorizationHttp('username', 'password');
$client->authorizationDigest('username', 'password');
$client->headers(['API-KEY' => 30]);
$client->header('API-KEY', 30);
$client->queryParams(['embed' => 'resource']);
$client->queryParam('embed', 'resource');
$client->jsonPayload([]);
$client->multipartPayload([]);
$client->formParamsPayload([]);
$client->getHeaders();
$client->getQueryParams();
$client->getBody();
$client->getRequestUrl('endpoint');
$response = $client->get('users');
$response = $client->post('users');
$response = $client->put('users/12');
$response = $client->delete('users/12');
$response = $client->patch('users/12');
$response = $client->head('users/12');
Response Methods
$response->statusCode(); // 200
$response->headers(); // []
$response->raw(); // ResponseInterface
$response->raw()->getHeaders(); // []
$response->isValid(); // true
$response->isServerError(); // false
$response->isClientError(); // false
$response->content(); // StdClass
$response->contentAsArray(); // Array
$response->contentAsJson(); // Json String
$response->contentAsCollection(); // Illuminate Collection
Use Case several requests
$client = (new HttpClient)
->baseUrl('https://example.com')
->authorizationBearer('your token');
// get all users
$response = $client
->queryParam('embed', 'client');
->get('users');
// update user
$response = $client
->jsonPayload(['name' => 'John'])
->put('users/' . $response->content()->data->id);
// set new Bearer
$client->authorizationBearer('another token');
// delete user
$response = $client->delete('users/id');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email systems@personal-business-machine.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.