软Servlet/互联网bs

此软件包的最新版本(dev-master)没有可用的许可证信息。

dev-master 2014-05-01 17:43 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:56:54 UTC


README

这是一个为 Internet.bs 域名注册商 API 定制的 PHP 客户端。此软件包还包含作为 laravel 4 模块运行的功能,但您也可以将其与任何框架或独立使用。

创建 Api 对象

use Softservlet\Internetbs\Api;
use Softservlet\Internetbs\Request\HttpRequest;

//The api credentials
$apiKey = 'testapi';
$passwd = 'testpass';
//We will use the test api in this example
$test = true;

$api = new Api(new HttpRequest($apiKey, $passwd, $test));

发送简单的 API 请求

API 请求通过 API 对象的 request() 方法进行。它接受 API 动词和要传递到请求中的数据作为参数。它以数组的形式返回响应。有关 API 动词(调用)的完整文档,请访问 internet.bs API 文档。

$response = $api->request('Domain/Check', array(
		'Domain' => 'github.com'
));

echo $response['status']; //this will display 'UNAVAILABLE'

使用 Command 类进行请求

命令是一种更有组织的进行 API 请求的方法。我们只需创建一个处理所需请求的命令,而不是将大量参数传递给 request() 方法。这也有助于您完成像创建域名这样的复杂任务。

创建新的命令

命令必须实现 Softservlet\Commands\CommandInterface 接口。

use Softservlet\Internetbs\Commands\CommandInterface;

class DomainCheck implements CommandInterface
{

	protected $domain;

	public function __construct($domain) 
	{
		$this->domain = $domain;
	}


	/**
	 * This method returns the api verb (call)
	 * that should be requested/executed by the command
	 */
	public function getPath()
	{
		return 'Domain/Check';
	}
	
	/**
	 * This method returns the data that should be passed
	 * to the api call
	 */
	public function run()
	{
		return array('Domain' => $this->domain);	
	}
}

使用命令

$response = $api->run(new DomainCheck('github.com'));

echo $response['status']; //this will display 'UNAVAILABLE'

DomainCreate 命令

此软件包已实现了 Domain/Create API 调用的任务。您可以随意分支此软件包,通过添加更多命令来贡献。

$command = new DomainCreate('the-domain-to-register.com');

假设注册域名的个人有以下详细信息

$contactPerson = array(
				'FirstName' => 'Benjamin',
				'LastName' => 'Dawson',
				'Email'	=> 'owner@name.com',
				'Street' => '1865 Shadowmar Drive',
				'CountryCode' => 'RO',
				'PostalCode' => '220099',
				'City' => 'Bucharest'	
);

然后我们可以将此人添加为 Registrant 个人。

$command->addContact('Registrant', $contactPerson);

但大多数顶级域(TLD)都需要像 RegistrantAdminTechnicalBilling 等角色的联系人。因此,我们可以使用以下方式添加具有多个角色的同一人。

$roles = array(
	'Registrant',
	'Admin',
	'Technical',
	'Billing'
);

$command->addContact($roles, $contactPerson);

电话号码字段需要特殊格式,因此您需要调用以下方法来处理它

$command->addPhoneNumber('Registrant', '+1', '310-716-7152');
//or
$command->addPhoneNumber($roles, '+1', '310-716-7152');

此命令扩展了 AbstractCommand 类,它提供了设置和获取 API 参数自定义数据的方法,因此如果您需要添加自定义数据,根据您想要注册的域名顶级域,只需使用

$command->setData('my_custom_data', 'my_value');

$command->getData('my_custom_data');

最后,只需运行命令

$api->run($command);

有关响应文档,请访问 internet.bs API 网站。