simukti / rest-client
A practical PHP 7 REST API client on-top-of pecl-http.
1.1.2
2017-07-31 10:35 UTC
Requires
- php: >=7.0.0
- ext-http: >=3.0.0
- ext-mbstring: *
- lcobucci/jwt: >=3.2.0,<4.0.0
This package is not auto-updated.
Last update: 2025-05-05 08:29:11 UTC
README
A practical PHP 7 REST API client on-top-of 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
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 use RestClient\HttpClient\PeclHttpClient; use RestClient\Request\ApiRequest; use RestClient\Response\ApiResponse; $request = new ApiRequest('https://api.github.com'); $request->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 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) ->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 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) ->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 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) ->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 use RestClient\HttpClient\PeclHttpClient; use RestClient\Request\ApiRequest; use RestClient\Response\ApiResponse; $request = new ApiRequest('https://httpbin.org'); $request->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 use RestClient\HttpClient\PeclHttpClient; use RestClient\Request\ApiRequest; use RestClient\Response\ApiResponse; $request = new ApiRequest('https://httpbin.org'); $request->setPath('/delete') ->setMethod(ApiRequest::METHOD_DELETE) ->addQuery('user_id', 1); $response = new ApiResponse; $httpClient = new PeclHttpClient; $httpClient->send($request, $response);
AUTHORIZATION
JWT
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 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('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
$response->isError(); // true|false (status >= 400) $response->isClientError(); // true|false (status 400 -> 499) $response->isServerError(); // true|false (status 500 -> 520)
Result
$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.