Skip to content

Instantly share code, notes, and snippets.

@librarywebchic
Last active May 19, 2017 16:51
Show Gist options
  • Save librarywebchic/b69ab176eb9257f1e75fe926620001e4 to your computer and use it in GitHub Desktop.
Save librarywebchic/b69ab176eb9257f1e75fe926620001e4 to your computer and use it in GitHub Desktop.
Example of calling NCIP service in PHP
/vendor/
/.project
/config.yml
{
"autoload" : {
"psr-0" : {
"OCLC" : "src"
}
},
"name" : "OCLC/DevNet/NCIPDemo",
"version" : "1.0",
"license" : "Apache-2.0",
"repositories" : [{
"type" : "git",
"url" : "https://github.com/OCLC-Developer-Network/oclc-auth-php.git"
}
],
"require" : {
"php" : ">=5.6",
"lib-curl" : "*",
"lib-openssl" : "*",
"ext-curl" : "*",
"ext-json" : "*",
"ext-openssl" : "*",
"guzzlehttp/guzzle" : ">=6.0",
"symfony/yaml" : "~2.3.7",
"OCLC/Auth" : "^4"
},
"config" : {
"bin-dir" : "vendor/bin"
},
"description" : "demonstration interacting with WMS NCIP - Patron profile",
"type" : "library",
"keywords" : [
"OCLC"
]
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "74100eeee72fe0d2ce454073bf2380fc",
"packages": [
{
"name": "OCLC/Auth",
"version": "4.1",
"source": {
"type": "git",
"url": "https://github.com/OCLC-Developer-Network/oclc-auth-php.git",
"reference": "8f16e0d4ae62396028e1cc666def615fd0df7bc6"
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-openssl": "*",
"guzzlehttp/guzzle": "^6",
"lib-curl": "*",
"lib-openssl": "*",
"php": ">=5.6"
},
"require-dev": {
"php-vcr/php-vcr": "1.3.1",
"php-vcr/phpunit-testlistener-vcr": "*",
"phpdocumentor/phpdocumentor": "2.*",
"phpunit/phpunit": "^5",
"zendframework/zend-log": "2.*"
},
"type": "library",
"autoload": {
"psr-0": {
"OCLC": "src"
}
},
"license": [
"Apache-2.0"
],
"description": "Authentication and Authorization Library for OCLC Web Services",
"keywords": [
"OCLC"
],
"time": "2017-04-11T20:56:15+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.2.3",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/8d6c6cc55186db87b7dc5009827429ba4e9dc006",
"reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006",
"shasum": ""
},
"require": {
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.4",
"php": ">=5.5"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.0",
"psr/log": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.2-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"rest",
"web service"
],
"time": "2017-02-28T22:50:30+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "v1.3.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"shasum": ""
},
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"time": "2016-12-20T10:07:11+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "1.4.2",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2017-03-20T17:10:46+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "symfony/yaml",
"version": "v2.3.42",
"target-dir": "Symfony/Component/Yaml",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "2cb5f366f9e0df014fc93de46cc416ba0a3055f8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/2cb5f366f9e0df014fc93de46cc416ba0a3055f8",
"reference": "2cb5f366f9e0df014fc93de46cc416ba0a3055f8",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
}
},
"autoload": {
"psr-0": {
"Symfony\\Component\\Yaml\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"time": "2016-05-30T08:10:17+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=5.6",
"lib-curl": "*",
"lib-openssl": "*",
"ext-curl": "*",
"ext-json": "*",
"ext-openssl": "*"
},
"platform-dev": []
}
<?xml version="1.0" encoding="UTF-8"?>
<NCIPMessage
xmlns="http://www.niso.org/2008/ncip"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ncip="http://www.niso.org/2008/ncip"
xmlns:ns2="http://oclc.org/WCL/ncip/2011/extensions"
xsi:schemaLocation="http://www.niso.org/2008/ncip http://www.niso.org/schemas/ncip/v2_01/ncip_v2_01.xsd"
ncip:version="http://www.niso.org/schemas/ncip/v2_01/ncip_v2_01.xsd">
<LookupUser>
<InitiationHeader>
<FromAgencyId>
<AgencyId ncip:Scheme="http://oclc.org/ncip/schemes/agencyid.scm">{institution}</AgencyId>
</FromAgencyId>
<ToAgencyId>
<AgencyId ncip:Scheme="http://oclc.org/ncip/schemes/agencyid.scm">{institution}</AgencyId>
</ToAgencyId>
<ApplicationProfileType ncip:Scheme="http://oclc.org/ncip/schemes/application-profile/wcl.scm">Version 2011</ApplicationProfileType>
</InitiationHeader>
<UserId>
<UserIdentifierValue>{principalID}</UserIdentifierValue>
</UserId>
<LoanedItemsDesired/>
<RequestedItemsDesired/>
<UserFiscalAccountDesired/>
<Ext>
<ns2:ResponseElementControl>
<ns2:ElementType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/elementtype.scm">Account Details</ns2:ElementType>
<ns2:StartElement> 1</ns2:StartElement>
<ns2:MaximumCount>10</ns2:MaximumCount>
<ns2:SortField ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/accountdetailselementtype.scm">Accrual Date</ns2:SortField>
<ns2:SortOrderType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/sortordertype.scm">Ascending</ns2:SortOrderType>
</ns2:ResponseElementControl>
<ns2:ResponseElementControl>
<ns2:ElementType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/elementtype.scm">Loaned Item</ns2:ElementType>
<ns2:StartElement>1</ns2:StartElement>
<ns2:MaximumCount>10</ns2:MaximumCount>
<ns2:SortField ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/loaneditemelementtype.scm">Date Due</ns2:SortField>
<ns2:SortOrderType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/sortordertype.scm">Ascending</ns2:SortOrderType>
</ns2:ResponseElementControl>
<ns2:ResponseElementControl>
<ns2:ElementType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/elementtype.scm">Requested Item</ns2:ElementType>
<ns2:StartElement>1</ns2:StartElement>
<ns2:MaximumCount>10</ns2:MaximumCount>
<ns2:SortField ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/requesteditemelementtype.scm">Date Placed</ns2:SortField>
<ns2:SortOrderType ncip:Scheme="http://worldcat.org/ncip/schemes/v2/extensions/sortordertype.scm">Ascending</ns2:SortOrderType>
</ns2:ResponseElementControl>
</Ext>
</LookupUser>
</NCIPMessage>
<?php
require_once('vendor/autoload.php');
use OCLC\Auth\WSKey;
use OCLC\User;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\Yaml\Yaml;
$config = YAML::parse(file_get_contents(__DIR__ . '/config.yml'));
$ncipMessage = file_get_contents(__DIR__ . '/lookupUser.xml');
$ncipMessage = str_replace('{principalID}', $config['principalID'], $ncipMessage);
$ncipMessage = str_replace('{institution}', $config['institution'], $ncipMessage);
$options = array('services' => array('WMS_NCIP'));
$wskey = new WSKey($config['key'], $config['secret'], $options);
$url = 'https://' . $config['institution'] . '.share.worldcat.org/ncip/circ-patron';
$user = new User($config['institution'], $config['principalID'], $config['principalIDNS']);
$accessToken = $wskey->getAccessTokenWithClientCredentials($config['institution'], $config['institution'], $user);
$client = new Client(
[
'curl' => [
CURLOPT_SSLVERSION => '3'
]]
);
$headers = array();
$headers['Authorization'] = 'Bearer ' . $accessToken->getValue();
try {
$response = $client->request('POST', $url, ['headers' => $headers, 'body' =>$ncipMessage]);
// parse XML returned
$lookupUserResponse = simplexml_load_string($response->getBody());
$lookupUserResponse->registerXPathNamespace('ns1', 'http://www.niso.org/2008/ncip');
$lookupUserResponse->registerXPathNamespace('wcl', 'http://oclc.org/WCL/ncip/2011/extensions');
$lookupUserResponse->registerXPathNamespace('usernote', 'http://www.oclc.org/ncip/usernote/2012');
echo '<p>Loaned Items</p>';
echo '<ul>';
foreach ($lookupUserResponse->xpath('//ns1:LoanedItem') as $loanedItem){
echo '<li>';
echo 'Title ' . $loanedItem->children('http://www.niso.org/2008/ncip')->Ext->BibliographicDescription->Title . '<br/>';
echo 'Due Date ' . $loanedItem->children('http://www.niso.org/2008/ncip')->DateDue . '<br/>';
echo '</li>';
}
echo '</ul>';
echo '<p>Holds</p>';
echo '<ul>';
foreach ($lookupUserResponse->xpath('//ns1:RequestedItem') as $requestedItem){
echo '<li>';
echo 'Request Type ' . $requestedItem->children('http://www.niso.org/2008/ncip')->RequestType . '<br/>';
echo 'Title ' . $requestedItem->children('http://www.niso.org/2008/ncip')->Title . '<br/>';
echo 'Hold Position ' . $requestedItem->children('http://www.niso.org/2008/ncip')->HoldQueuePosition . '<br/>';
echo 'Status ' . $requestedItem->children('http://www.niso.org/2008/ncip')->RequestStatusType . '<br/>';
echo '</li>';
}
echo '</ul>';
echo '<p>Account Balance</p>';
echo $lookupUserResponse->children('http://www.niso.org/2008/ncip')->LookupUserResponse->Ext->children('http://oclc.org/WCL/ncip/2011/extensions')->UserFiscalAccountSummary->children('http://www.niso.org/2008/ncip')->AccountBalance->MonetaryValue / 100 . ' ';
echo $lookupUserResponse->children('http://www.niso.org/2008/ncip')->LookupUserResponse->Ext->children('http://oclc.org/WCL/ncip/2011/extensions')->UserFiscalAccountSummary->children('http://www.niso.org/2008/ncip')->AccountBalance->CurrencyCode;
} catch (RequestException $error) {
echo $error->getResponse()->getStatusCode();
echo $error->getResponse()->getBody(true);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment