corex/client

此包已被弃用,不再维护。作者建议使用 guzzlehttp/guzzle 包。

各种客户端(curl-wrapper, rest-client等)

2.0.3 2019-03-17 08:08 UTC

This package is auto-updated.

Last update: 2022-02-01 13:01:22 UTC


README

PHP的各种客户端(Http, Rest等)

License Build Status codecov

支持的方法:get, post, put, delete, patch, options。

典型流程为...

  • 在客户端上创建和设置属性。
  • 在请求上创建和设置属性。
  • 使用请求调用客户端(客户端和请求上的属性将合并)。
  • 使用响应获取数据(存在各种方法来获取数据)。

请注意,客户端可能会抛出异常。

基本客户端(抽象)

所有客户端都扩展了基本客户端,这意味着将有一个通用的方法集可用。

  • 可以在构造函数中指定URL。
  • baseUrl() - 指定基本URL(覆盖通过构造函数设置的基本URL)。
  • timeout() - 指定以秒为单位的超时时间。
  • token() - 指定令牌。令牌用于在路径中指定{},例如,路径可以是"/user/{id}",调用token('id', 4')将生成"/user/4"。
  • param() - 指定参数。例如:param('param', 'test')将添加为[?/&]param=test在URL上。所有参数都将进行URL编码。
  • header() - 指定请求头。例如:header('Accept', 'application/json')。
  • userAgent() - 指定用户代理。
  • getDebug() - 获取调试信息(不返回响应)。

通常,在请求上设置令牌、参数和头将覆盖客户端上的令牌、参数和头。但是,可以在客户端上指定它们为final。

基本请求(抽象)

  • path() - 指定路径。将添加到URL中。
  • token() - 指定令牌(在别处解释)。如果设置为final,将覆盖客户端。
  • param() - 指定参数(在别处解释)。如果设置为final,将覆盖客户端。
  • header() - 指定头(在别处解释)。如果设置为final,将覆盖客户端。

基本响应(抽象)

  • header() - 获取响应头。
  • headers() - 获取响应头。
  • body() - 获取正文。
  • status() - 获取状态(HTTP状态)。如果请求成功,则返回200。通过类Status获取消息。

Http\Client(扩展基本客户端)

从基本客户端继承的方法。

一些示例。

// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Http\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com');
$request = (new Http\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

Http\Request(扩展基本请求)

从基本请求继承的方法+以下...

  • body() - 设置正文。

一些示例。

// Create request with path and token on request.
$request = new Http\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Http\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Http\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with body set.
$request = new Http\Request();
$request->body('{"something":["test1","test2"]}');
var_dump($request);

Http\Response(扩展基本响应)

从基本响应继承的方法。

一些示例。

// Get body from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

Rest\Client(扩展基本客户端)

从基本客户端继承的方法。

一些示例。

// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Rest\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com');
$request = (new Rest\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

Rest\Request(扩展基本请求)

从基本请求继承的方法+以下...

  • field() - 设置字段,例如 field('firstname', 'Roger');

一些示例。

// Create request with path and token on request.
$request = new Rest\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Rest\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Rest\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with fields.
$request = new Rest\Request();
$request->field('firstname', 'Roger');
$request->field('lastname', 'Moore');
var_dump($request);

Rest\Response (继承自基本响应)

从基本响应继承的方法 + 以下...

  • value() - 从响应中获取值。使用点表示法,例如 value('actor.firstname');
  • toArray() - 转换为数组。

一些示例。

// Get body from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

// Get title from response. Dot notation supported.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->value('title'));

// Get response as array.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->toArray());

Rest\Entity (抽象) / Rest\Collection (抽象)

存在许多方法用于从集合中提取数据。查看 corex/support 以查看集合上的可用方法。添加了以下方法...

  • toArray() - 转换为数组。

Entity 和 Collection 的使用示例。

class Album extends Entity
{
    public $userId;
    public $id;
    public $title;
}

class Albums extends Collection
{
    public function current()
    {
        return new Album(parent::current());
    }
}

// Using above classes will make you able to iterate over album/albums and have auto-completion.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/albums');
$response = $client->call(Method::GET);
$albums = new Albums($response);
foreach ($albums as $album) {
    print($album->title . "\n");
    var_dump($album->toArray());
}