thalvik/resdiary-api-client

ResDiary REST API 的 PHP 客户端

dev-master 2018-04-12 12:17 UTC

This package is auto-updated.

Last update: 2024-09-08 06:39:09 UTC


README

这是一个 ResDiary API 的非官方 PHP 客户端。它包含基本的 HTTP 客户端,这是一个简单的 Guzzle HTTP 客户端库包装器。然后通过调用可用的端点与 ResDiary API 进行通信。请查看 ResDiary API 文档以了解可用的 API 调用。

基本用法

运行 composer

composer require thalvik/resdiary-api-client

使用参数初始化类

use Thalvik\ResDiaryApiClient\RdaClient;

$rdaClient = new RDAClient([
	'api_url' => 'https://thegreatapi.com/', //Change this to value given by ResDiary
	'username' => 'someusername', //Change this to value given by ResDiary
	'password' => 'somepassword' //Change this to value given by ResDiary
]);

由于 token 有效期为 24 小时,您可能希望检查是否已经保存了 token,例如在自定义函数中检查数据库

$tokenSaved = someFunctionToRetrieveSaved24hToken('YOUR_RESDIARY_TOKEN_NAME');

然后您可以在客户端上调用 setAccessToken(),如果 token 已过期,将设置新的 token 并检索它

$tokenClient = $rdaClient->setAccessToken($tokenSaved);

然后您可以检查 token 是否已更改,通过在自定义函数中调用,为您设置新的 24 小时 token

if ($tokenClient != $tokenSaved) {
	someFunctionToSave24hToken( 'YOUR_RESDIARY_TOKEN_NAME', $tokenClient);
}

然后您可以使用客户端调用可用的服务方法

$now = new \DateTime();

$restaurantSetup = $rdaClient->getConsumerService('Restaurant') //Service name
->setMicroSiteName('TestProvider') //Set microSiteName
->getSetup([ //Call method
	'date' => $now->format('Y-m-d'),
	'channelCode' => 'ONLINE',
]);

如果客户端请求中有错误,您可以通过调用 hasErrorsgetErrors 方法进行检查。getErrors 将返回包含 MessageValidationErrors 的数组

if ($rdaClient->hasErrors()) {
	print_r($rdaClient->getErrors());
	exit();
}

否则,调用将返回相关数据

print_r($restaurantSetup);

要运行测试,请将凭据填入 phpunit.xml.dist 文件

<const name="BASE_URI" value=""/>
<const name="USERNAME" value=""/>
<const name="PASSWORD" value=""/>

然后运行 PHPUnit

vendor/bin/phpunit