danmichaelo / ncip
基本NCIP库
Requires
- php: >=5.3.0
- danmichaelo/quitesimplexmlelement: ~0.2.0
- evenement/evenement: 1.0.*
- illuminate/support: ~4.1.0
- nesbot/carbon: ~1.8.0
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: 3.7.*
- satooshi/php-coveralls: dev-master
README
php-ncip是一个用于解析和格式化NCIP请求和响应消息的PHP包。开发指导原则是追求简洁的API而不是完整的API。目前仅覆盖了NCIP规范的一小部分,但欢迎提出添加建议。
安装
Composer
将包添加到composer.json
文件中的require
列表。
{ "require": { "danmichaelo/ncip": "dev-master" }, }
然后运行composer install
以获取包的最新版本。
Laravel 4的附加步骤
该包附带一个Laravel 4服务提供者,如果您喜欢,可以安装它。它包含一个配置文件,因此您可以在那里设置设置,而不是将它们传递给构造函数。
要注册服务提供者,请将'Scriptotek\NcipServiceProvider',
添加到app/config/app.php
中的providers
列表。然后在终端中运行php artisan config:publish danmichaelo/ncip
以创建配置文件app/config/packages/danmichael/ncip/config.php
。
使用
构造
要构造客户端,您需要指定NCIP服务的URL、客户端的任意用户代理字符串和机构ID。机构ID标识您的机构,如NCIP协议中所述。
require_once('vendor/autoload.php'); use Scriptotek\Ncip\NcipConnector, Scriptotek\Ncip\NcipClient; $ncipUrl = 'http://eksempel.com/NCIPResponder'; $userAgent = 'My NCIP client/0.1'; $agencyId = 'a'; $conn = new NcipConnector($ncipUrl, $userAgent, $agencyId); $client = new NcipClient($conn);
要构造服务器
require_once('vendor/autoload.php'); use Scriptotek\Ncip\NcipServer; $server = new NcipServer;
如果您已注册Laravel 4服务提供者,则可以通过应用程序容器构造类
$client = App::make('ncip.client'); $server = App::make('ncip.server'); // Or if you have access to an instance of the application. $client = $app['ncip.client']; $server = $app['ncip.server'];
现在设置从app/config/packages/danmichael/ncip/config.php
中提取,而NcipConnector
会自动注入到NcipClient
中。
客户端示例
$user = $client->lookupUser('abc123'); if ($user->exists) { echo 'Hello ' . $user->firstName . ' ' . $user->lastName; } else { echo 'User not found'; }
服务器示例
$postData = file_get_contents('php://input'); $request = $server->parseRequest($postData); if ($request->is('LookupUser')) { $response = new UserResponse; $response->userId = $request->userId; $response->firstName = 'Meriadoc'; $response->lastName = 'Brandybuck'; echo $response->xml(); }
事件
客户端在向服务器发送每个请求时都会触发事件。这可以用于实现日志记录。事件包括request.item
、request.user
、request.checkout
、request.checkin
和request.renew
。
$client->on('request.checkout', function($userId, $itemId) { $log->addInfo('Requested checkout of item "' . $itemId . '" for user "' . $userId . '"'); }
对于调试,在向服务器发送和从服务器接收每个消息时都会发出事件message.send
和message.recv
。消息XML体作为第一个参数提供。
$client->on('message.send', function($msg) { printf("[SEND] %s\n", $msg); } $client->on('message.recv', function($msg) { printf("[RECV] %s\n", $msg); }