collectiveaccess/service-wrapper

CollectiveAccess 网络服务 API 的简单 PHP 封装器

1.1.5 2023-01-12 16:26 UTC

This package is not auto-updated.

Last update: 2024-09-17 21:15:57 UTC


README

请访问 https://www.collectiveaccess.org 获取更多信息,并参考 https://manual.collectiveaccess.org 了解服务 API 和核心软件的其他功能。

基本用法

您可以通过 composer 安装此库,并利用 composer 灵活的自动加载功能。从那里,您可以直接使用 CollectiveAccessService 命名空间中的所有服务类。

例如

$client = new CollectiveAccessService\ItemService("https:///","ca_objects","GET",1);
$result = $client->request();
print_r($result->getRawData());

这将为您获取对象记录(object_id 为 1)的通用摘要。

这里有一些其他服务端点的简单示例,以帮助您入门

$vo_client = new CollectiveAccessService\ModelService("https:///","ca_entities");
$vo_client->setRequestBody(array("types" => array("corporate_body")));
$vo_result = $vo_client->request();

$vo_result->isOk() ? print_r($vo_result->getRawData()) : print_r($vo_result->getErrors());
$vo_client = new CollectiveAccessService\SearchService("https:///","ca_objects","*");
$vo_client->setRequestBody(array(
	"bundles" => array(
		"ca_objects.access" => array("convertCodesToDisplayText" => true),
		"ca_objects.status" => array("convertCodesToDisplayText" => true),
		"ca_entities.preferred_labels.displayname" => array("returnAsArray" => true)
	)
));
$vo_result = $vo_client->request();

$vo_result->isOk() ? print_r($vo_result->getRawData()) : print_r($vo_result->getErrors());

认证

要使用认证,您基本上有 3 个选项。第一个是使用 PHP 常量 __CA_SERVICE_API_USER____CA_SERVICE_API_KEY__,如下一个示例所示,如果您想在同一个脚本中运行多个服务请求,这将非常有用。

请注意,所有 3 个认证选项都尝试从远程服务中检索 authToken,并将其保存在临时目录中,只要它是有效的就重用它。当它过期时,它将使用以下 3 个选项之一提供的用户名和密钥进行重新认证。在此期间,用户/密钥不被使用。

现在回到第一个选项——常量

define('__CA_SERVICE_API_USER__', 'administrator');
define('__CA_SERVICE_API_KEY__', 'dublincore');

$o_service = new CollectiveAccessService\ItemService('https://', 'ca_objects', 'GET', 1);
$o_result = $o_service->request();

您还可以使用简单的设置器

$o_service = new CollectiveAccessService\ItemService('https://', 'ca_objects', 'GET', 1);
$o_service->setCredentials('administrator', 'dublincore');
$o_result = $o_service->request();

第 3 个选项(可能是最适合生产的)是将凭据作为环境变量 CA_SERVICE_API_USERCA_SERVICE_API_KEY 传递。想象这个简单的脚本为 authtest.php

$o_service = new CollectiveAccessService\ItemService('https://', 'ca_objects', 'GET', 1);
$o_result = $o_service->request();

然后,在终端中运行类似以下命令应该可以工作

export CA_SERVICE_API_USER=administrator
export CA_SERVICE_API_KEY=dublincore
php authtest.php

要在 web 服务器设置中这样做,您可以查看 apache 的 mod_env

重试失败的连接

从版本 1.1.3 开始,封装器可以自动重试失败的连接。在这种情况下,“失败”发生在无法建立连接或请求正在进行中时连接被终止。返回 HTTP 错误(如 401(需要认证))的请求不会被重试。

默认情况下,不会尝试重试。使用 setRetries() 方法设置放弃之前最大尝试次数。使用 setRetryDelay() 方法设置重试之间的时间延迟。时间以毫秒为单位。

例如,要使用最多 5 次重试和两次秒的延迟连接到项目服务

$client = new CollectiveAccessService\ItemService("https:///","ca_objects","GET",1);
$result = $client->setRetries(5)->setRetryDelay(2000)->request();
print_r($result->getRawData());