dormilich/arin-client

一个用于与 ARIN 数据库通信的 PHP 库。

1.1.1 2017-10-05 08:17 UTC

This package is auto-updated.

Last update: 2024-09-21 23:48:05 UTC


README

一个用于与 ARIN 数据库 (Reg-RWS) 通信的 PHP 库。

设置数据对象

设置数据对象有众多可能性。

  • XML 叶节点(例如 <city>)使用 setValue()
  • 集合元素(如 <comment><originASes>)也支持 addValue()
  • 所有有效载荷都使用 set()add() 与元素名称一起。
  • 有效载荷和集合元素实现了 ArrayAccess 接口。也就是说,你可以像多维数组一样遍历 XML 结构(其中集合表示数值索引和有效载荷关联数组)。注意,你只能通过数组访问设置命名叶节点的值(例如,下面的例子中 $net['ASN']$net['net'])。
use Dormilich\WebService\ARIN\Elements\Element;
use Dormilich\WebService\ARIN\Payloads\Customer;
use Dormilich\WebService\ARIN\Payloads\Country;
use Dormilich\WebService\ARIN\Payloads\Net;
use Dormilich\WebService\ARIN\Payloads\NetBlock;

$customer = new Customer;

// adding simple values
$customer
  ->set('city', 'MyTown')
  ->set('postalCode', 12345)
;
// set values array style
$customer['city'] = 'AnyTown';

// delete values
unset($customer['city']);

// some elements know what to save
$customer['private'] = 'on';
var_dump($customer['private']->getValue()); // bool(true)
// …even if you use their alias
echo $customer['private']->getName(); // customerPrivate

// set up sub-payloads…
// …partially…
$customer['country']['code2'] = 'US';
// …or at once
$country = new Country;
$country['code3'] = 'USA';
$country['e164'] = 1;       # that’s the country calling code, btw.
$customer['country'] = $country;

// set up multi-line elements’ values…
$customer
  ->add('comment', 'line 1')
  ->add('comment', 'line 2')
;
$customer['comment'][] = 'line 3';
$customer['comment']->addValue('line 4');

// …edit them…
$customer['comment'][3] = 'LINE 4';

// …or delete selected ones
unset($customer['comment'][2]);

// element groups work similar (but you have to know what to put in!)
$net = new Net;
$net['ASN'][0] = Element::createWith('originAS', 'AS-007');

// and of course they are editable
$net['net'][0] = new NetBlock;
$net['net'][0]['start'] = '192.168.10.32';
$net['net'][0]['end']   = '192.168.10.63';

这些数据对象执行一些基本验证(你不能将对象放入期望字符串的字段中,反之亦然,一些与类型相关的字段只允许预定义值),但通常你必须知道什么属于哪里。

发生异常的情况有:

  • 无效的值类型
  • 值约束违反
  • 访问不存在的字段

设置网络服务

为了使网络服务工作,你需要一个实现 ClientAdapter 接口的对象。这可以是一个现有的库,或者你可以自己编写连接功能(尽管我建议选择第一种方案)。

要配置网络服务本身,有四个选项可以设置:

  • 环境:可以是“生产”数据库的“live”,或者“测试”数据库的“test”。
  • 密码:你访问相应数据库的 API 密钥。
  • 编码:你要发送的 XML 的编码字符集。默认为 UTF-8。
  • 严格:如果你想绕过预序列化有效性检查,请将此选项设置为 FALSE。默认为 TRUE。

使用网络服务

有两个网络服务对象可用:用于处理通过票据(票据、报告和 ROA)处理的任何内容的 TicketRWS,以及与 CRUD 操作(如将网络分配给客户)相关的 CommonRWS

use Dormilich\WebService\ARIN\WebService\CommonRWS;
use Dormilich\WebService\ARIN\Payloads\Customer;
use Dormilich\WebService\ARIN\Payloads\Net;

$client = new MyClient(…);
$arin = new CommonRWS($client, [
    'environment' => 'live',
    'password'    => 'my-arin-password',
]);

/* set up customer */

$customer = new Customer;

# set up customer object…

// don’t ask me why customers have to be newly created for every net
$customer = $arin->create($customer, 'PARENT-NET-HANDLE');

/* set up net with that customer */

$net = new Net;

# assign network properties, among that…
$net['customer']  = $customer->getHandle();
$net['parentNet'] = 'PARENT-NET-HANDLE';

// don’t ask me why there is a need for a wrapper
$response = $arin->create($net);

// mind that a network assignment will result 
// in a ticket if the automated process failed.
try {
    $net = $response['net'];
}
catch (Exception $e) {
    $ticket = $response['ticket'];
}

// alternately fetch the first element
$net = $response[0];

if ($net instanceof Net) {
    # net successfully assigned
}

错误处理

错误处理取决于你的连接对象如何处理 HTTP 错误。如果 Reg-RWS 返回错误有效载荷,你可以通过 Payload::loadXML() 将其转换为对象,或者如果你没有抛出异常,你可以使用从网络服务调用中接收到的对象。

注意

所有的有效载荷和组元素都是可迭代的,因此可以直接在 foreach() 循环中使用。此外,有效载荷可以序列化为 JSON。

所有的元素都可以转换为字符串。如果该元素关联有 XML 属性,你可以将其作为对象的属性访问。