simukti/rest-client

基于 pecl-http 的实用 PHP 7 REST API 客户端。

1.1.2 2017-07-31 10:35 UTC

This package is not auto-updated.

Last update: 2024-09-23 05:40:35 UTC


README

这是一个基于 pecl-http 的实用 PHP 7 REST API 客户端。

特性

  • 包含授权头生成器(基本、Bearer、JWT),可选自定义前缀值
  • 根据请求内容类型自动生成 JSON 主体
  • 可配置 HTTP 客户端和请求选项(基于 pecl-http 选项)
  • 内置支持管道请求(pecl-http 功能)
  • 通过响应状态码检查客户端/服务器错误
  • 默认请求接受编码为 gzip, deflate,如果服务器响应被 gzip 或 deflate 压缩,则自动解压

需求

安装

simukti/rest-client 添加到您的 composer.json 中

    "require": {
        "simukti/rest-client": "^1.1.0"
    }

或通过内联 composer 命令添加最新版本

composer require simukti/rest-client -vvv -o

请求

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(上传文件)

<?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(原始数据)

<?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);

授权

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

这是如果您从 API 服务器获取令牌的示例。

<?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->isError(); // true|false (status >= 400)
$response->isClientError(); // true|false (status 400 -> 499)
$response->isServerError(); // true|false (status 500 -> 520)

结果

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

许可证

本项目遵循 MIT 许可证发布。