REST 客户端,用于执行 GET、POST、PUT、DELETE、PATCH 等调用。

v1.5.0 2023-10-10 09:32 UTC

This package is auto-updated.

Last update: 2024-09-10 11:38:15 UTC


README

Tests Latest Stable Version License Total Downloads

REST 客户端,用于执行 GET、POST、PUT、DELETE、PATCH 等。

安装

要安装此包,我们只需将依赖项添加到 composer.json 文件中。

{
  "require": {
    "othercode/rest": "*"
  }
}

然后运行以下命令

composer install

用法

要使用 Rest,我们只需实例化它并配置我们想要的参数。我们可以通过访问 ->configuration->configure_property 来设置配置,例如,要配置调用的 URL,我们只需设置 ->configuration->url 参数,如下所示

$api = new \OtherCode\Rest\Rest();
$api->configuration->url = "https://jsonplaceholder.typicode.com/";

或者

$api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
    'url' => 'https://jsonplaceholder.typicode.com/',
)));

之后,我们需要设置调用的类型和我们将使用的参数,在这种情况下,我们将对 "posts/1" 端点执行 GET 请求。

$response = $api->get("posts/1");

如果与连接有关的问题,REST 客户端将抛出 ConnectionException

注意:这些错误与 cURL 会话相关,以下是 完整的错误列表

方法

可用于操作的方法有

get()

执行 GET 请求。

返回:响应对象

head()

执行 HEAD 请求。

返回:响应对象(无主体)

post()

执行 POST 请求。

返回:响应对象

delete()

执行 DELETE 请求。

返回:响应对象

put()

执行 PUT 请求。

返回:响应对象

patch()

执行 PATCH 请求。

返回:响应对象

getMetadata()

返回请求的元数据。

返回:数组

getError()

返回最后一个已知错误。

返回Error 对象

getPayloads()

返回包含 ResponseRequest 对象的数组。

返回:数组

setDecoder()

设置新的解码器。

返回:Rest 对象

setEncoder()

设置新的编码器。

返回:Rest 对象

setModule()

设置新的模块。

返回:Rest 对象

unsetModule()

注销模块。

返回:Rest 对象

addHeader()

添加新的头。

返回:Rest 对象

addHeaders()

添加头数组。

返回:Rest 对象

注意:我们可以使用 addHeader()addHeaders() 方法与 Rest 实例或 configuration 对象一起使用

$api->addHeader('some_header','some_value');
$api->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));

等同于

$api->configuration->addHeader('some_header','some_value');
$api->configuration->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));

removeHeader()

移除头偏移。

返回:Rest 对象

removeHeaders()

移除头数组。

返回:Rest 对象

模块

此包允许您创建模块以在请求前后执行任务。要创建新模块,我们只需使用此模板

class CustomModule extends BaseModule
{
    public function run()
    {
        // do something
    }
}

重要:任何模块都必须扩展 BaseModule

必须实现的方法是 ->run(),此方法执行模块的自定义代码。

一旦我们有我们的模块,我们可以使用 ->setModule() 方法将其注册。此方法需要三个参数,第一个是模块的名称,第二个是模块的完整命名空间,第三个是我们模块的钩子名称,它可以 "before" 和 "after",这取决于我们想要何时启动我们的模块。

$api->setModule('module_name','Module\Complete\Namespace','after');

对于 "before" 模块,您可以使用请求对象的全部属性。

  • 方法
  • url
  • headers
  • body

对于 "after" 模块,您可以使用响应对象的全部属性。

  • code
  • 内容类型
  • 字符集
  • body
  • headers
  • 错误
  • 元数据

所有模块都是按照我们在 Rest 客户端中注册的顺序执行的,这也影响到解码器和编码器。

解码器

解码器是一种模块,允许您自动将响应解码为 xml 或 json。要使用它们,我们只需要使用 ->setDecoder() 方法设置我们想要的解码器

$api->setDecoder("json");

此方法的默认允许值是:jsonxmlxmlrpc。所有解码器总是在 "after" 钩子中执行。

自定义解码器

要创建一个新的解码器,我们只需要使用此模板

class CustomDecoder extends BaseDecoder
{
	protected $contentType = 'application/json';

	protected function decode()
	{
		// decode $this->body
	}
}

与模块一样,我们有响应对象可供使用。$contentType 属性是触发解码器的 content-type,在上面的例子中,所有 content-type 为 "application/json" 的响应都会触发此解码器。

快速调用

我们可以使用 \OtherCode\Rest\Payloads\Request::call() 方法进行快速调用。这个静态方法返回一个 Rest 实例,因此我们可以使用它所有的方法。

$response = \OtherCode\Rest\Payloads\Request::call('https://jsonplaceholder.typicode.com')
    ->setDecoder('json')
    ->get('/posts/1');

完整示例

require_once '../autoload.php';

try {

    $api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
        'url' => 'https://jsonplaceholder.typicode.com/',
        'httpheader' => array(
            'some_header' => 'some_value',
        )
    )));
    $api->setDecoder("json");
    
    $response = $api->get("posts/1");
    var_dump($response);
    
} catch (\Exception $e) {
    print "> " . $e->getMessage() . "\n"
}