承接 simukti/rest-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simukti/rest-client

Composer 安装命令:

composer require simukti/rest-client

包简介

A practical PHP 7 REST API client on-top-of pecl-http.

README 文档

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.

simukti/rest-client 适用场景与选型建议

simukti/rest-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 simukti/rest-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 simukti/rest-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 simukti/rest-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-14