danmichaelo / ncip
Basic NCIP library
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 is a php package for parsing and formatting NCIP request and response messages. Development has been guided by a desire for a simple API rather than a complete one. Only a small subset of the NCIP specification is currently covered, but suggestions for additions are welcome.
Installation:
Composer
Add the package to the require
list of your composer.json
file.
{ "require": { "danmichaelo/ncip": "dev-master" }, }
and run composer install
to get the latest version of the package.
Additional steps for Laravel 4
The package comes with a Laravel 4 service provider that you can install if you like. It comes with a config file, so you can set settings there instead of passing them to the constructor.
To register the service provider, add 'Scriptotek\NcipServiceProvider',
to the list of providers
in app/config/app.php
. Then run php artisan config:publish danmichaelo/ncip
in your terminal to create the config file app/config/packages/danmichael/ncip/config.php
.
Usage:
Construction
To construct a client, you need to specify the url to the NCIP srvice, a freely choosen user agent string for your client, and the agency id. The agency id identifies your institution, as specified in the NCIP protocol.
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);
To construct a server:
require_once('vendor/autoload.php'); use Scriptotek\Ncip\NcipServer; $server = new NcipServer;
If you have registered the Laravel 4 service provider, the classes can be constructed through the applications container instead:
$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'];
The settings are now pulled from app/config/packages/danmichael/ncip/config.php
instead, and the NcipConnector
is injected into the NcipClient
automatically.
Client example:
$user = $client->lookupUser('abc123'); if ($user->exists) { echo 'Hello ' . $user->firstName . ' ' . $user->lastName; } else { echo 'User not found'; }
Server example:
$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(); }
Events:
The client emits events on each request to the server. This can be useful to implement logging.
The events are request.item
, request.user
, request.checkout
, request.checkin
and request.renew
.
$client->on('request.checkout', function($userId, $itemId) { $log->addInfo('Requested checkout of item "' . $itemId . '" for user "' . $userId . '"'); }
For debugging, events message.send
and message.recv
are emitted on each message sent to and received
from the server. The message xml body is given as the first argument.
$client->on('message.send', function($msg) { printf("[SEND] %s\n", $msg); } $client->on('message.recv', function($msg) { printf("[RECV] %s\n", $msg); }