项目作者: simukti

项目描述 :
PHP 7 REST API client on-top-of pecl-http.
高级语言: PHP
项目地址: git://github.com/simukti/rest-client.git
创建时间: 2016-09-14T07:45:37Z
项目社区:https://github.com/simukti/rest-client

开源协议:

下载


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

  1. "require": {
  2. "simukti/rest-client": "^1.1.0"
  3. }

OR add require latest version via inline composer command

  1. composer require simukti/rest-client -vvv -o

REQUEST

GET

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://api.github.com');
  6. $request->setPath('/users/simukti/repos')
  7. ->addQuery('sort', 'updated')
  8. ->addQuery('type', 'owner')
  9. ->addHeader('accept', 'application/json');
  10. // default http method is GET
  11. $response = new ApiResponse;
  12. $httpClient = new PeclHttpClient;
  13. $httpClient->send($request, $response);

POST

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/post')
  7. ->setMethod(ApiRequest::METHOD_POST)
  8. ->addData('username', 'kadalkesit')
  9. ->addData('password', 'superkesit')
  10. ->addQuery('foo', 'bar')
  11. ->addQuery('baz', 'bat');
  12. // application/x-www-form-urlencoded
  13. $response = new ApiResponse;
  14. $httpClient = new PeclHttpClient;
  15. $httpClient->send($request, $response);

POST (UPLOAD FILES)

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/post')
  7. ->setMethod(ApiRequest::METHOD_POST)
  8. ->addData('user_id', 100)
  9. ->addFile('picture', '/path/to/your/file_to_upload.extension');
  10. $oken = 'your_generated_token';
  11. $authorization = new \RestClient\Authorization\BearerStringAuthorization($githubToken);
  12. $request->setAuthorization($authorization);
  13. $response = new ApiResponse;
  14. $httpClient = new PeclHttpClient;
  15. $httpClient->send($request, $response);

POST (Raw Data)

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/post')
  7. ->setMethod(ApiRequest::METHOD_POST)
  8. ->setDataRaw(json_encode(['key' => 'value1', 'key2' => [ 'subkey2.1' => 'subkey2.1 value'] ]))
  9. ->addHeader('content-type', 'application/json');
  10. $httpClient = new PeclHttpClient;
  11. $response = $httpClient->send($request, new ApiResponse);

PUT

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/put')
  7. ->setMethod(ApiRequest::METHOD_PUT)
  8. ->addData('username', 'kadalkesit')
  9. ->addData('password', 'superkesit')
  10. ->addQuery('foo', 'bar')
  11. ->addQuery('baz', 'bat')
  12. ->addHeader('content-type', 'application/json');
  13. // json body
  14. $response = new ApiResponse;
  15. $httpClient = new PeclHttpClient;
  16. $httpClient->send($request, $response);

DELETE

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/delete')
  7. ->setMethod(ApiRequest::METHOD_DELETE)
  8. ->addQuery('user_id', 1);
  9. $response = new ApiResponse;
  10. $httpClient = new PeclHttpClient;
  11. $httpClient->send($request, $response);

AUTHORIZATION

JWT

  1. use RestClient\HttpClient\PeclHttpClient;
  2. use RestClient\Request\ApiRequest;
  3. use RestClient\Response\ApiResponse;
  4. $request = new ApiRequest('https://httpbin.org');
  5. $request->setPath('/post')
  6. ->setMethod(ApiRequest::METHOD_POST)
  7. ->setData([
  8. 'username' => 'kadalkesit',
  9. 'password' => 'superkesit'
  10. ])
  11. ->addQuery('expand', 'user,role');
  12. $simpleJWT = new \RestClient\Authorization\JWTAuthorization('key_as_ISS', 'secret', [
  13. 'jti' => 'jtid',
  14. 'scope' => [
  15. 'user', 'writer'
  16. ]
  17. ]);
  18. $request->setAuthorization($simpleJWT);
  19. $response = new ApiResponse;
  20. $httpClient = new PeclHttpClient;
  21. $httpClient->send($request, $response);

Bearer

This is example if you have token from api server.

  1. <?php
  2. use RestClient\HttpClient\PeclHttpClient;
  3. use RestClient\Request\ApiRequest;
  4. use RestClient\Response\ApiResponse;
  5. $request = new ApiRequest('https://httpbin.org');
  6. $request->setPath('/post')
  7. ->setMethod(ApiRequest::METHOD_POST)
  8. ->setData(
  9. [
  10. 'username' => 'kadalkesit',
  11. 'password' => 'superkesit',
  12. ]
  13. )
  14. ->addQuery('include_refresh_token', 0);
  15. $githubToken = 'your_generated_token';
  16. $githubAuthHeader = new \RestClient\Authorization\BearerStringAuthorization($githubToken);
  17. $request->setAuthorization($githubAuthHeader);
  18. $response = new ApiResponse;
  19. $httpClient = new PeclHttpClient;
  20. $httpClient->send($request, $response);

RESPONSE

Error Checking

  1. $response->isError(); // true|false (status >= 400)
  2. $response->isClientError(); // true|false (status 400 -> 499)
  3. $response->isServerError(); // true|false (status 500 -> 520)

Result

  1. $response->getContentType(); // application/json, text/html, text/plain, application/xml
  2. $response->getContent(); // get result body (string)
  3. $response->getHeaders(); // response header

LICENSE

This project is released under the MIT licence.