robsonsanches/stays-php-client

用于与 Stays API v1(非官方库)服务器端集成的 PHP 客户端

v1.0.0 2022-02-20 21:08 UTC

This package is auto-updated.

Last update: 2024-09-21 03:07:10 UTC


README

用于与 Stays API v1(非官方库)服务器端集成的 PHP 客户端

GitHub release (latest by date) GitHub

目录

要求

  • PHP >= 7.3
  • Composer

安装

composer require robsonsanches/stays-php-client

入门

要获取 API 凭据,请阅读 Stays API 文档 https://stays.net/external-api/#introduction

设置

Stays API 集成设置

require '/vendor/autoload.php';

use RobsonSanches\Stays\Client\Client;

$stays = new Client($domain, $clientId, $clientSecret, $options);

客户端参数

附加选项

发送请求

您可以使用 get、post、patch 和 delete 方法向 Stays API 发送请求。

请求方法

$stays->get($endpoint, $query = [], $headers = [])
$stays->post($endpoint, $data = [], $headers = [])
$stays->patch($endpoint, $data = [], $headers = [])
$stays->delete($endpoint, $query = [], $headers = [])

参数

使用响应

所有请求方法在成功时将返回一个响应,该响应可以是 多维数组对象数组JSON 字符串。如果存在参数 http_errors = true,在失败时将抛出 ClientException 错误。

use RobsonSanches\Stays\Client\ClientException;

try {
    $results = $stays->get('content/groups');
    print_r( $results, true ); // array or JSON string

} catch (ClientException $e) {
    echo $e->getMessage(); // Exception message.
}

从 Http 客户端获取响应数据

$response = $stays->http()->getResponse();

echo $response->getReasonPhrase(); // Reason phrase (string).
echo $response->getStatusCode(); // Response code (int).
echo $response->getBody()->getContents(); // Response body (JSON).

print_r( $response->getHeaders() ); // Response headers (array).
print_r( $response->getBody() ); // PSR-7 StreamInterface (object).

如果您需要获取最后请求的数据

$request = $stays->http()->getRequest();

echo $request->getUri(); // Requested URI (string).
echo $request->getMethod(); // Request method (string).
echo $request->getBody()->getContents(); // Request body content (JSON).

print_r( $request->getHeaders() ); // Request headers (array).
print_r( $request->getUri() ); // PSR-7 UriInterface (object).
print_r( $request->getBody() ); // PSR-7 StreamInterface (object).

文档

Stays 文档

许可证

MIT 许可证

发布历史

  • 2022-02-20 - 1.0.0 - 初次发布。