PHP库,用于访问RGP API

v1.0.0 2020-05-08 23:29 UTC

This package is not auto-updated.

Last update: 2024-09-21 20:48:56 UTC


README

截至2019年12月,Rock Gym Pro已发布了一个基本的API。此PHP类旨在轻松访问所有提供的端点。RGP已明确表示,未来的开发将取决于社区的兴趣和使用情况,所以开始编码吧!如果您希望API中添加某些功能,请通知RGP支持,因为这将有助于指导开发。

API文档

您可以在以下位置查看RGP的API文档: https://api.rockgympro.com

API密钥

在能够访问API之前,您需要生成一个API密钥。RGP的以下Google文档提供了如何为云服务器和本地托管服务器生成密钥的说明。

本库的1.0.0版本

本库的范围已缩小,仅提供基本的包装和一些对响应结构的微小调整。之前,对于API的每个端点都有单独的方法。API中的多次更改都破坏了库,因此删除了针对特定端点的方法,转而采用更通用的方法,使用新的get()方法。

安装

使用此库有两种方式。您可以使用Composer将其安装为依赖项,或者您可以从此存储库下载RGP.php并将其包含在您的代码中。

Composer

composer require reganface/rgp

在项目顶部包含Composer的autoload文件以加载所有依赖项。

require __DIR__ . '/vendor/autoload.php';

下载

将RGP.php保存到您的项目文件夹中,并将其包含在您的项目中。

require "/path/to/RGP.php";

cURL

此类需要安装并启用您所使用的PHP版本的cURL。如果cURL不可用,PHP将引发错误。

基本用法

在实例化类时,您只需包括您的API用户名和API密钥。任何错误都会抛出异常,因此请确保在try/catch块中保留代码。

$api_username = "apiname";	// add your api username here
$api_key = "apikey";		// add your api key here

try {
	$rgp = new reganface\RGP\RGP($api_username, $api_key);

	// do stuff

} catch (Exception $e) {
	// handle errors
}

get()返回的数据结构将略有不同,如果您直接访问API,这与RGP返回的数据结构略有不同。这样做是为了将响应的元数据与您请求的数据分开。结构如下所示

[
	"data"  => [],			// The requested data
	"response"  => [],		// response information
	"pages"  => []			// pagination information (if applicable)
]

如果您希望数据与API返回的完全相同,可以使用get_raw()代替。

示例

// Get some data. In this case, a list of all your facilities.
$result = $rgp->get("/facilities");

// The data will always be found in the "data" key.
$facilities  =  $result["data"];

// Output a list of each facility's three character code and the facility name.
if (!empty($facilities)) {
	foreach($facilities as $facility) {
		echo  "{$facility["code"]} - {$facility["name"]}\n";
	}
}

方法

get()

get (string $path [, array $params])

返回API数据,数据结构略有修改,如下所示

[
	"data" => array,			// this is the data you are looking for
	"response" => array,		// meta data about the response
	"pages" => [				// this will be here for any endpoints that return paginated data
		"itemTotal" => int,		// total number of results
		"itemPage" => int,		// number of results per page
		"pageTotal" => int,		// total number of pages
		"pageCurrent" => int	// the current page that was just returned
	]
]

path
这是API端点的路径。所有当前端点的列表可在https://api.rockgympro.com找到。注意:路径中不要包含"/v1"。

params
一个关联数组,包含您想要在调用中包含的任何查询参数。

get_raw()

get_raw (string $path [, array $params])

此方法与get()相同,但它将返回API返回的数据。

test()

test ()

测试到API的连接。如果可以成功建立连接,则返回true。