Last active
May 15, 2017 13:30
-
-
Save SimonXIX/8ff484d04f8f36283801 to your computer and use it in GitHub Desktop.
Script to display real-time availability of items in Ex Libris Alma. This was used as a proof-of-concept for a webpage to check the availability of borrowable laptops in LapSafe self-service laptop lockers. N.b.: only works on bibs with a single item record.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# @name: item_checker.php | |
# @version: 0.3 | |
# @license: GNU General Public License version 3 (GPLv3) <https://www.gnu.org/licenses/gpl-3.0.en.html> | |
# @purpose: Display real-time availability information for laptops for loan at Imperial College London Library | |
# @author: Simon Barron <[email protected]> | |
# @acknowledgements: Based on a script by Chris Keene available at https://gist.github.com/chriskeene | |
?> | |
<html> | |
<head> | |
<meta http-equiv="refresh" content="300"> | |
<link rel="stylesheet" href="laptops.css"> | |
<title>Laptop availability - Imperial College London</title> | |
</head> | |
<body> | |
<table> | |
<tr> | |
<th>Locker</th> | |
<th>Barcode</th> | |
<th>Availability</th> | |
</tr> | |
<?php | |
$mms00 = urlencode('xxxxxxxxxxxxxx'); | |
$mms01 = urlencode('xxxxxxxxxxxxxx'); | |
$mms02 = urlencode('xxxxxxxxxxxxxx'); | |
$mms03 = urlencode('xxxxxxxxxxxxxx'); | |
$mms04 = urlencode('xxxxxxxxxxxxxx'); | |
$mms05 = urlencode('xxxxxxxxxxxxxx'); | |
$mms06 = urlencode('xxxxxxxxxxxxxx'); | |
$mms07 = urlencode('xxxxxxxxxxxxxx'); | |
$mms08 = urlencode('xxxxxxxxxxxxxx'); | |
$mms09 = urlencode('xxxxxxxxxxxxxx'); | |
$mmsarray = [$mms00,$mms01,$mms02,$mms03,$mms04,$mms05,$mms06,$mms07,$mms08,$mms09]; | |
function curlMultiRequest($urls) { | |
$ch = array(); | |
$results = array(); | |
$mh = curl_multi_init(); | |
foreach($urls as $key => $val) { | |
$ch[$key] = curl_init(); | |
curl_setopt($ch[$key],CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch[$key], CURLOPT_URL, $val); | |
//curl_setopt ($ch[$key], CURLOPT_FAILONERROR,1); | |
// follow line enables https | |
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false); | |
curl_multi_add_handle($mh, $ch[$key]); | |
} | |
$running = null; | |
do { | |
$status = curl_multi_exec($mh, $running); | |
$info = curl_multi_info_read($mh); | |
if ( $info['result'] != 0 ) { | |
//print_r($info); | |
} | |
} | |
while ($running > 0); | |
// Get content and remove handles. | |
foreach ($ch as $key => $val) { | |
$results[$key] = curl_multi_getcontent($val); | |
curl_multi_remove_handle($mh, $val); | |
} | |
curl_multi_close($mh); | |
return $results; | |
} | |
function getItems(){ | |
global $mmsarray; | |
$apikey = urlencode('xxxxxxxxxxxxxx'); | |
$baseurl = 'https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/'; | |
$format = 'json'; | |
$count=0; | |
$holdinglist = array(); // for holding the holdings | |
$items = array(); // what we will return | |
$urllist = array(); | |
// prepare loans urls | |
// GET /almaws/v1/bibs/{mms_id}/loans | |
$paramurl = '?apikey=' . $apikey . '&format=' . $format; | |
foreach ($mmsarray as $key => $value){ | |
$urllist['loans'][$key] = $baseurl . $value . '/loans' . $paramurl; | |
} | |
// prepare holdings urls | |
// GET /almaws/v1/bibs/{mms_id}/holdings | |
$suffixurl = '/holdings?apikey=' . $apikey . '&format=' . $format; | |
foreach ($mmsarray as $key => $value){ | |
$urllist['holdings'][$key] = $baseurl . $value . $suffixurl; | |
} | |
// get loans and holdings from api | |
// returns an array, keys are loans/holdings. | |
// values are the json responses | |
$loans = curlMultiRequest($urllist['loans']); | |
$holdings = curlMultiRequest($urllist['holdings']); | |
$loaninfo = array(); | |
// loans json | |
foreach ($loans as $key => $value){ | |
$json = json_decode($value, true); | |
$loans[$key] = $json['item_loan'][0]['due_date']; | |
} | |
// set mms ids as array keys | |
$loans = array_combine($mmsarray, $loans); | |
// holdings json | |
foreach ($holdings as $key => $value){ | |
$json = json_decode($value, true); | |
$holdings[$key] = $json['holding'][0]['holding_id']; | |
} | |
// set mms ids as array keys | |
$holdings = array_combine($mmsarray, $holdings); | |
// for each holdings id, prepare item urls | |
// GET /almaws/v1/bibs/{mms_id}/holdings/{holding_id}/items | |
$suffixurl = '/items?apikey=' . $apikey . '&format=' . $format; | |
foreach ($holdings as $key => $value) { | |
$itemurls[$key] = $baseurl . $key . '/holdings/' . $value . $suffixurl; | |
} | |
// call each of the item urls at the same time | |
$results3 = curlMultiRequest($itemurls); | |
$results3 = array_combine($lockerarray, $results3); | |
$items = array(); | |
foreach ($results3 as $key => $value){ | |
// create json object based on the item json | |
$item = json_decode($value, true); | |
$items[$key]['title'] = (string) $item['item'][0]['bib_data']['title']; | |
$items[$key]['barcode'] = (string) $item['item'][0]['item_data']['barcode']; | |
$items[$key]['availability'] = (string) $item['item'][0]['item_data']['base_status']['value']; | |
if ($items[$key]['availability']) { | |
$items[$key]['status'] = 'Available'; | |
} else { | |
$items[$key]['status'] = 'Not Available'; | |
} | |
$items[$key]['duedate'] = date('G:i l j F', strtotime($loans[$key])); | |
"<tr><td>" . $items[$key]['title'] . "</td><td>" . $items[$key]['barcode'] . "</td>"; | |
if ($items[$key]["status"] === 'Not Available'){ | |
print "<td class='unavailable'>" . "Due: " . $items[$key]['duedate'] . "</td></tr>"; | |
} | |
else { | |
print "<td class='available'>" . $items[$key]['status'] . "</td></tr>"; | |
} | |
} | |
} | |
getItems(); | |
?> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment