dialogue1/amity-client

amity 的 API 客户端

dev-master / 1.x-dev 2014-12-04 20:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:11:41 UTC


README

本软件包包含一个基于 Guzzle3 的 amity API (v2) 客户端。如果您想将项目与 amity 安装连接并管理联系人、列表等,请使用此软件包。

要求

  • PHP 5.3
  • Composer 用于安装软件包及其依赖项

安装

使用 Composer,安装过程非常简单

php composer.phar require dialogue1/amity-client:~1.0

在您的 PHP 代码中,您只需包含由 Composer 生成的自动加载器

require 'vendor/autoload.php';

$apiClient = dialogue1\amity\API\Client::create(...);

使用方法

通常,您需要以下四项信息来使用 amity API

  • 安装的主机名(例如 news.mydomain.com
  • 是否使用 SSL
  • amity 客户端 ID(例如 gh26bb81
  • 您的 amity API 密钥(例如 34hguj13fggj1kjh24j5hg45j1h3g2g42

您可以选择实例化一个新的 API 客户端,它会根据您的需求设置所有依赖项,或者使用普通的 Guzzle 对象并将 amity API 逻辑注入其中。

使用 API 客户端

// This will setup a new Guzzle client and prepare it for the amity API
$apiClient = dialogue1\amity\API\Client::create(
   'news.mydomain.com',
   true,
   'gh26bb81',
   '34hguj13fggj1kjh24j5hg45j1h3g2g42'
);

API 客户端提供了对提供的 API 端点的包装,并为您处理错误。处理资源(如联系人)的代码位于 services 中。

$apiClient      = dialogue1\amity\API\Client::create(...);
$contactService = $apiClient->getContactService();

// both $contacts and $females will be a PHP array of contacts
$contacts = $contactService->getMany();
$females  = $contactService->getMany(null, null, null, null, 'f');

$newbie = $contactService->create(array(
	'email' => 'some@one.org'
));

如果现有的包装不足以满足需求(例如,如果您想使用资源嵌入),您可以使用 requestData() 辅助方法。

$apiClient = dialogue1\amity\API\Client::create(...);
$contacts  = $apiClient->requestData('GET', '/contacts', array('embed' => 'lists'));

使用现有的 Guzzle HTTP 客户端

或者,只需使用 RequestSigner 插件向任何 Guzzle 实例添加签名功能 - 在这种情况下,您需要自己设置主机名和 SSL

$httpClient = getGuzzleClientFromMyExistingFramework();
$signer     = new dialogue1\amity\API\RequestSigner('<client ID>', '<api key>');

$httpClient->addSubscriber($signer);

使用这种设置,您可以按照自己的喜好执行自由请求。请注意,您必须自己处理解码 JSON、处理错误等!

// [snip]

$contacts = $httpClient->get('/contacts');

许可证

本代码根据 MIT 许可证授权。