crapougnax/client

Arrowsphere API 的 PHP 客户端

dev-devel 2016-01-18 13:37 UTC

This package is not auto-updated.

Last update: 2024-09-11 23:09:55 UTC


README

Arrowsphere API 客户端套件是消费 Arrowsphere xAC API 提供的服务最佳方式。

使用 PHP 编写且框架无关,客户端以 composer 包的形式提供。

xAC 客户端

xAC 客户端是一个完整的 PHP 语义客户端,能够对 xAC API 执行查询。

配置

客户端配置非常简单。设置您的 API 密钥、API URL 和所需的 API 版本。

use Arrowsphere\Client\xAC as Client;

Client::setApiKey('MySecretApiKeyThatNobodyKnowsAbout');

Client::setBaseUrl('https://xac.arrowsphereconnect.com/api/');

Client::setVersion(2); // 1 and 2 (default) are valid versions

客户端实用方法

获取最后响应

使用 getLastRespons() 静态方法从服务中获取最后响应。内容是一个表示最新接收到的 json 有效负载的数组。

var_dump(Client::getLastResponse());

array(6) {
  ["status"]=>
  int(0)
  ["message"]=>
  string(7) "Success"
  ["ACUserID"]=>
  int(1)
  ["ACTransactionID"]=>
  string(36) "7d985f56-011f-436e-bf38-4afec9467cc6"
  ["language"]=>
  string(5) "en_UK"
  ["body"]=>
  array(4) {
    ["parameters"]=>
    array(0) {
    }
    ["data"]=> ...
    

获取服务

获取整个 API 和由 API 密钥标识的用户或给定 Entity() 实例可用的服务数组。

// Get all services for user ahtentified with API key
var_dump(Client::getServices());

array(15) {
  ["transactions"]=>
  array(3) {
    ["type"]=>
    string(10) "collection"
    ["description"]=>
    string(16) "Get Transactions"
    ["endpoint"]=>
    string(12) "transactions"
  }
  ["customers"]=>
  array(3) {
    ["type"]=>
    string(10) "collection"
    ["description"]=>
    string(39) "Get Customers of authenticated reseller"
    ["endpoint"]=>
    string(9) "customers"
  }
  ...
  
var_dump(Client::getServices(Client::customer()));

光标

基本用法

$cursor = Client::customers();

// get first customers batch as an array of xAC\Entity() instances
$collection = $cursor->get();

// or walk through collection
$cursor->rewind();
foreach ($cursor->getOne() as $entity) {
	// do something on $entity instance
}

当前方法

// get next batch
$data = $cursor->next();

// get previous page
$data = $cursor->prev();

查询过滤器

// add filter on-the-fly
$cursor->filter('name', 'Acme')->get();

实体

基本用法

实体是像客户、订阅或订单这样的元素。每个元素都可以作为 xAC\Entity() 实例访问。

// Get a customer instance
$customer = Client::customer('myRef');

// Get a property
echo $customer->company_name;

实体光标

实体实例可以包含集合。要访问数据,获取一个光标,并按“光标”部分所述进行操作。

// Get a cursor for the subscriptions collection
$cursor = $customer->subscriptions();

// get first batch
$batch = $cursor->get();

实体操作

要执行给定 Entity() 实例上的操作(无论是填充的还是空的),获取一个 Action() 处理程序实例。

// create an empty customer instance
$customer = Client::customer();

// get a create action handler
$action = $customer->create();

// or do the same in one pass
$action = Client::customer()->create();

Action() 实例可以使用 setData() 方法接收数据数组。

$action->setData(['someKey' => 'someValue']);

然后可以使用 execute() 方法执行 Action()。

// $result will contain a boolean
$result = $action->execute();
创建并保存新元素
// create an empty customer instance
$customer = Client::customer();

// get a provision action
$action = $customer->provision();

$action->setData([
	'customer_ref'  => 'newRef',
	'customer_name' => 'Acme Limited',
	...
]);

$res = $action->execute();

// send a create request to the API with customer data
$result = $customer->create([
	'customer_ref'  => 'newRef',
	'customer_name' => 'Acme Limited',
	...
]);