kazami-labs / ripedb-client
用于与 RIPE NCC 数据库通信的 PHP 库。
Requires
- php: ^5.4 || 7.0 - 7.2
Requires (Dev)
- guzzlehttp/guzzle: ~6.0
- phpunit/phpunit: ^5.4
This package is not auto-updated.
Last update: 2024-10-02 04:43:20 UTC
README
用于与 RIPE NCC 数据库通信的 PHP 库。
要求
RIPEDB-Client 需要 PHP 5.4 到 PHP 7.2。连接对象可能还有其他前提条件。
安装
您可以通过 composer 安装 RIPE 客户端
composer require kazami-labs/ripedb-client
或通过克隆此仓库。
测试
要运行离线测试,请在项目的根目录中从命令行运行
phpunit
如果您有互联网连接,还可以运行在线测试,该测试在 RIPE TEST 数据库上执行一些基本操作。
phpunit --group live
如果这些测试因为 HTTP 状态码为 409 而失败,则表示所使用的 IP 范围(127.0.0.0/31)已存在于 TEST 数据库中,需要在再次运行测试之前将其删除。
设置
为了创建与 RIPE REST 服务器的 HTTP 连接,您需要创建一个实现 ClientAdapter
接口的连接对象。根据您的 PHP 版本或您的个人偏好,您可以使用任何现有的库或使用 PHP 的 curl 或套接字函数编写一个。
如果您觉得这样做不方便,您可以使用 Guzzle6Adapter
,它位于 tests/Test
文件夹中(尽管您可能想更改命名空间)。但是,请注意,Guzzle 6 需要 PHP 5.5 或更高版本。
用法
在使用之前,您可以为该 web 服务设置两个选项
- 环境 - 是否连接到 RIPE(《WebService::PRODUCTION》)或 TEST(《WebService::SANDBOX》)数据库。默认情况下,它连接到 TEST 数据库。
- 密码 - 对于任何修改操作(创建/更新/删除),您必须提供对象维护者的密码。默认密码是 TEST 数据库主维护者的密码。
有关更多信息,请参阅 RIPE REST API 文档 和 RIPE 数据库文档
设置 web 服务对象
// create the connection object $client = new Client(…); // create the web service object $ripe = new WebService($client, [ 'environment' => WebService::PRODUCTION, 'password' => 'your maintainer password', ]); // you can set also these options separately $ripe = new WebService($client); $ripe->setEnvironment(WebService::PRODUCTION);
创建 RIPE DB 条目
try { // create a RIPE object $me = new Person; // setting attributes via array style $me['person'] = 'John Doe'; // setting multiple-valued attributes via array style $me['phone'] = [ '+1 234 56789', '+1 432 98765', ]; // setting attributes via method $me ->addAttribute('address', 'Any Street 1') ->addAttribute('address', 'Anytown') ; // create object in DB $me = $ripe->create($me); // display result echo '<pre>', $me, '</pre>'; } catch (RPSLException $e) { // errors regarding the setup of the RIPE object } // using Guzzle 6 exceptions as example, // your client implementation may use different exceptions catch (BadResponseException $e) { $errors = WebService::getErrors($e->getResponse()->getBody())); }
注意:webservice 将根据其设置设置 source 属性,因此您不需要自己设置它,除非您想在使用序列化器或 isValid()
方法之前设置它。
更新 RIPE DB 条目
try { // create a RIPE object with the object’s primary key $jd = new Person('JD123-RIPE'); // fetch the object from the DB $jd = $ripe->read($jd); // modify the object $jd['e-mail'] = 'john.doe@example.com'; // save the changes $jd = $ripe->update($jd); } catch (RPSLException $e) { // errors regarding the setup of the RIPE object } catch (BadResponseException $e) { $errors = WebService::getErrors($e->getResponse()->getBody())); }
注意:每个 RIPE 对象至少包含 created 和 last-modified 生成的属性。后者显然只有在更新后才是实际的,因此 update() 返回最新的对象实例。
RIPE 参考
某些属性包含对其他 RIPE 对象的引用(例如 tech-c、admin-c、mnt-*)。当您从 RIPE 数据库获取对象时,对于这些属性,将创建一个特殊值对象(AttributeValue
),它可以提供引用对象的类型和查找对象(其主键设置为在 read()
方法中使用的对象)。
RIPE 注释
RIPE DB 使用井号(#
)表示注释。当获取带有注释的属性时,这些注释将单独从属性值中传输。在这种情况下,将使用 AttributeValue
对象。当将属性值作为字符串访问时,注释将被附加。
注释
对象验证使用 RIPE DB 版本 1.86