tcdent/php-restclient 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tcdent/php-restclient

Composer 安装命令:

composer require tcdent/php-restclient

包简介

A generic REST API client for PHP

README 文档

README

Total Downloads

https://github.com/tcdent/php-restclient
(c) 2013-2022 Travis Dent tcdent@gmail.com

Installation

Requires PHP 7.2 or above. Use 0.1.7 for legacy versions of PHP.

$ composer require tcdent/php-restclient

Basic Usage

$api = new RestClient([
    'base_url' => "https://api.twitter.com/1.1", 
    'format' => "json", 
     // https://dev.twitter.com/docs/auth/application-only-auth
    'headers' => ['Authorization' => 'Bearer '.OAUTH_BEARER], 
]);
$result = $api->get("search/tweets", ['q' => "#php"]);
// GET http://api.twitter.com/1.1/search/tweets.json?q=%23php
if($result->info->http_code == 200)
    var_dump($result->decode_response());

Configurable Options

headers - An associative array of HTTP headers and values to be included in every request.
parameters - An associative array of URL or body parameters to be included in every request.
curl_options - cURL options to apply to every request; anything defined here: https://secure.php.net/manual/en/function.curl-setopt.php. These will override any automatically generated values.
build_indexed_queries (bool) - http_build_query automatically adds an array index to repeated parameters which is not desirable on most systems. Use this option to enable the default behavior. Defaults to FALSE. user_agent - User agent string to use in requests.
base_url - URL to use for the base of each request.
format - Format string is appended to resource on request (extension), and used to determine which decoder to use on response; a request URL like "api.twitter.com/1.1/statuses/user_timeline.json" would be expected to return well-formed JSON.
format_regex - Pattern to extract format from response Content-Type header, used to determine which decoder to use on response.
decoders - Associative array of format decoders. See "Direct Iteration and Response Decoding".
username - Username to use for HTTP basic authentication. Requires password.
password - Password to use for HTTP basic authentication. Requires username.

Options can be set upon instantiation, or individually afterword:

$api = new RestClient([
    'format' => "json", 
    'user_agent' => "my-application/0.1"
]);

-or-

$api = new RestClient;
$api->set_option('format', "json");
$api->set_option('user_agent', "my-application/0.1");

Standard Verbs

Four HTTP verbs are implemented as convenience methods: get(), post(), put() and delete(). Each accepts three arguments:

url (string) - URL of the resource you are requesting. Will be prepended with the value of the base_url option, if it has been configured. Will be appended with the value of the format option, if it has been configured.

parameters (string), (array) - String or associative array to be appended to the URL in GET requests and passed in the request body on all others. If an array is passed it will be encoded into a query string. A nested, indexed array is passed for parameters with multiple values and will be iterated to populate duplicate keys See: "Duplicate Headers and Parameters"

headers (array) - An associative array of headers to include with the request. A nested, indexed array is passed for parameters with multiple values and will be iterated to populate duplicate keys See: "Duplicate Headers and Parameters"

Other Verbs

You can make a request using any verb by calling execute() directly, which accepts four arguments: url, method, parameters and headers. All arguments expect the same values as in the convenience methods, with the exception of the additional method argument:

method (string) - HTTP verb to perform the request with.

Response Details

After making a request with one of the HTTP verb methods, or execute, the returned instance will have the following data populated:

response (string)- The raw response body content. See "Direct Iteration and Response Decoding" for ways to parse and access this data.

headers (object) - An object with all of the response headers populated. Indexes are transformed to snake_case for access. Duplicate headers are available as an indexed array under the shared key.

$response->headers->content_type;
$response->headers->x_powered_by;

info (object) - An object with information about the transaction. Populated by casting curl_info to an object. See PHP documentation for more info: http://php.net/manual/en/function.curl-getinfo.php Available attributes are:

url, content_type, http_code, header_size, request_size, filetime, 
ssl_verify_result, redirect_count, total_time, namelookup_time, connect_time, 
pretransfer_time, size_upload, size_download, speed_download, speed_upload, 
download_content_length, upload_content_length, starttransfer_time, redirect_time, 
certinfo, primary_ip, primary_port, local_ip, local_port, redirect_url  

error (string) - cURL error message, if applicable.

response_status_lines (array) - Indexed array of raw HTTP response status lines. See: "Multiple HTTP Status Lines".

Direct Iteration and Response Decoding

If the the response data format is supported, the response will be decoded and accessible by iterating over the returned instance. When the format option is set, it will be used to select the decoder. If no format option is provided, an attempt is made to extract it from the response Content-Type header. This pattern is configurable with the format_regex option.

$api = new RestClient([
    'base_url' => "http://vimeo.com/api/v2", 
    'format' => "php"
]);
$result = $api->get("tcdent/info");
// GET http://vimeo.com/api/v2/tcdent/info.php
foreach($result as $key => $value)
    var_dump($value);

Reading via ArrayAccess has been implemented, too:

var_dump($result['id']);

To access the decoded response as an array, call decode_response().

'json' and 'php' formats are configured to use the built-in json_decode and unserialize functions, respectively. Overrides and additional decoders can be specified upon instantiation, or individually afterword. Decoder functions take one argument: the raw request body. Lambdas and functions created with create_function work, too.

function my_xml_decoder($data){
    new SimpleXMLElement($data);
}

$api = new RestClient([
    'format' => "xml", 
    'decoders' => ['xml' => "my_xml_decoder"]
]);

-or-

$api = new RestClient;
$api->set_option('format', "xml");
$api->register_decoder('xml', "my_xml_decoder");

Or, using a lambda; this particular example allows you to receive decoded JSON data as an array.

$api->register_decoder('json', function($data){
    return json_decode($data, TRUE);
});

Duplicate Headers and Parameters

When duplicate (repeated) HTTP headers are received, they are accessible via an indexed array referenced by the shared key. Duplicated headers and parameters may also be constructed in requests using the same format.

Example (unlikely) response:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Type: text/html; charset=UTF-8

Accessing repeated headers in the response instance:

$result = $api->get('/');
var_dump($result->headers->content_type);

=> ["text/html", "text/html; charset=UTF-8"]

Passing repeated headers and parameters in a request:

$result = $api->get('/', [
    'foo[]' => ['bar', 'baz']
], [
    'Accept' => ['text/json', 'application/json']
]);

Will create headers and a query string (GET) or response body (POST, etc) with the following content:

GET /?foo[]=bar&foo[]=baz HTTP/1.1
Accept: text/json
Accept: application/json

Multiple HTTP Status Lines

Multiple status lines returned in a single response payload are supported, and available as response_status_lines which is an indexed array populated on the response instance.

Example response with multiple status lines (truncated):

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Cache-Control: no-cache
...
$result = $api->get('/');
var_dump($result->response_status_lines);

=> ["HTTP/1.1 100 Continue", "HTTP/1.1 200 OK"]

JSON Verbs

This library will never validate or construct PATCH JSON content, but it can be configured to communicate well-formed data.

PATCH JSON content with correct content type:

$result = $api->execute("http://httpbin.org/patch", 'PATCH',
    json_encode([foo' => 'bar']), [
        'X-HTTP-Method-Override' => 'PATCH', 
        'Content-Type' => 'application/json-patch+json']);

Tests

The test package includes a simple server script which returns debug information for verifying functionality. Start the server first, then run tests:

$ php -S localhost:8888 RestClientTest.php
$ phpunit RestClientTest.php
  • Requires PHP > 5.5.7 in order for getallheaders data to populate.
  • If you specify an alternate port number or hostname to the PHP server you need to re-configure it in your phpunit.xml file:
<php><var name="TEST_SERVER_URL" value="http://localhost:8888"/></php>

tcdent/php-restclient 适用场景与选型建议

tcdent/php-restclient 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.06M 次下载、GitHub Stars 达 352, 最近一次更新时间为 2015 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「curl」 「json」 「xml」 「rest」 「api」 「client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 tcdent/php-restclient 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.06M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 353
  • 点击次数: 17
  • 依赖项目数: 34
  • 推荐数: 0

GitHub 信息

  • Stars: 352
  • Watchers: 26
  • Forks: 174
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-13