Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simukti/rest-client
PHP 7 REST API client on-top-of pecl-http.
https://github.com/simukti/rest-client
curl pecl-http php rest
Last synced: 7 days ago
JSON representation
PHP 7 REST API client on-top-of pecl-http.
- Host: GitHub
- URL: https://github.com/simukti/rest-client
- Owner: simukti
- Created: 2016-09-14T07:45:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-31T10:40:40.000Z (over 7 years ago)
- Last Synced: 2024-11-16T17:16:22.574Z (about 1 month ago)
- Topics: curl, pecl-http, php, rest
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## README
A practical PHP 7 REST API client on-top-of [pecl-http](https://pecl.php.net/package/pecl_http).## FEATURES
- Include authorization header generator (basic, bearer, jwt) with optional custom prefix value
- Auto json body based on request content-type
- Configurable http client and request options (based on pecl-http options)
- Built-in pipelined request support (pecl-http feature)
- Client/server error check by response status code
- Default request accept-encoding is `gzip, deflate` and auto inflate if server response is gziped or deflated## REQUIREMENTS
- [php >=7.0.0](https://secure.php.net/)
- [pecl-http >=3.0.0](https://pecl.php.net/package/pecl_http)
- [mbstring](http://php.net/manual/en/book.mbstring.php)## INSTALL
Add `simukti/rest-client` to your composer.json```
"require": {
"simukti/rest-client": "^1.1.0"
}
```OR add require latest version via inline composer command
```
composer require simukti/rest-client -vvv -o
```## REQUEST
### GET
```php
setPath('/users/simukti/repos')
->addQuery('sort', 'updated')
->addQuery('type', 'owner')
->addHeader('accept', 'application/json');// default http method is GET
$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```### POST
```php
setPath('/post')
->setMethod(ApiRequest::METHOD_POST)
->addData('username', 'kadalkesit')
->addData('password', 'superkesit')
->addQuery('foo', 'bar')
->addQuery('baz', 'bat');
// application/x-www-form-urlencoded
$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```### POST (UPLOAD FILES)
```php
setPath('/post')
->setMethod(ApiRequest::METHOD_POST)
->addData('user_id', 100)
->addFile('picture', '/path/to/your/file_to_upload.extension');$oken = 'your_generated_token';
$authorization = new \RestClient\Authorization\BearerStringAuthorization($githubToken);
$request->setAuthorization($authorization);$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```### POST (Raw Data)
```php
setPath('/post')
->setMethod(ApiRequest::METHOD_POST)
->setDataRaw(json_encode(['key' => 'value1', 'key2' => [ 'subkey2.1' => 'subkey2.1 value'] ]))
->addHeader('content-type', 'application/json');$httpClient = new PeclHttpClient;
$response = $httpClient->send($request, new ApiResponse);
```### PUT
```php
setPath('/put')
->setMethod(ApiRequest::METHOD_PUT)
->addData('username', 'kadalkesit')
->addData('password', 'superkesit')
->addQuery('foo', 'bar')
->addQuery('baz', 'bat')
->addHeader('content-type', 'application/json');
// json body
$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```### DELETE
```php
setPath('/delete')
->setMethod(ApiRequest::METHOD_DELETE)
->addQuery('user_id', 1);$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```
### AUTHORIZATION
#### JWT
```php
use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
->setMethod(ApiRequest::METHOD_POST)
->setData([
'username' => 'kadalkesit',
'password' => 'superkesit'
])
->addQuery('expand', 'user,role');$simpleJWT = new \RestClient\Authorization\JWTAuthorization('key_as_ISS', 'secret', [
'jti' => 'jtid',
'scope' => [
'user', 'writer'
]
]);
$request->setAuthorization($simpleJWT);$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```#### Bearer
This is example if you have token from api server.```php
setPath('/post')
->setMethod(ApiRequest::METHOD_POST)
->setData(
[
'username' => 'kadalkesit',
'password' => 'superkesit',
]
)
->addQuery('include_refresh_token', 0);$githubToken = 'your_generated_token';
$githubAuthHeader = new \RestClient\Authorization\BearerStringAuthorization($githubToken);
$request->setAuthorization($githubAuthHeader);$response = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);
```## RESPONSE
### Error Checking
```php
$response->isError(); // true|false (status >= 400)
$response->isClientError(); // true|false (status 400 -> 499)
$response->isServerError(); // true|false (status 500 -> 520)
```### Result
```php
$response->getContentType(); // application/json, text/html, text/plain, application/xml
$response->getContent(); // get result body (string)
$response->getHeaders(); // response header
```## LICENSE
This project is released under the MIT licence.