Skip to content

Instantly share code, notes, and snippets.

@chriskeene
Last active Aug 27, 2015
Embed
What would you like to do?
Vufind Alma.php sample first attempt getHolding code
<?php
$start_time = MICROTIME(TRUE);
//error_reporting(0);
function curlMultiRequest($urls, $options = array()) {
$ch = array();
$results = array();
$mh = curl_multi_init();
foreach($urls as $key => $val) {
$ch[$key] = curl_init();
curl_setopt($ch[$key],CURLOPT_RETURNTRANSFER,1);
// follow line enables https
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$key], CURLOPT_URL, $val);
curl_multi_add_handle($mh, $ch[$key]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
}
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 getHolding($id) {
$mmsid = $id;
$apikey = urlencode('xxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$baseurl = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/';
$count=0;
$holdinglist = array(); // for holding the holdings
$items = array(); // what we will return
$urllist = array();
// prepare loans url
$paramurl = '?apikey=' . $apikey;
$urllist['loans'] = $baseurl . urlencode($mmsid) . '/loans' . $paramurl;
// prepare holdings url
$suffixurl = '/holdings?apikey=' . $apikey;
$urllist['holdings'] = $baseurl . urlencode($mmsid) . $suffixurl;
// get loans and holdings from api
// returns an array, keys are loans/holdings.
// values are the xml responses
$results2 = curlMultiRequest($urllist);
$loaninfo = array();
// loans xml
$record = new SimpleXMLElement($results2["loans"]);
// process loans xml creating array with barcode as key
$loans = array();
// if no loans there will be no item_loan
if ($record) {
foreach ($record->item_loan as $itemloan) {
$duedate = $itemloan->due_date;
$barcode = $itemloan->item_barcode;
$loans["$barcode"] = $duedate;
}
}
// now get each holdings id number
//$hids = array();
$record = new SimpleXMLElement($results2["holdings"]);
$holdinglist = array();
$datafields = $record->xpath('/holdings/holding/holding_id');
foreach ($datafields as $hid) {
$holdinglist[] = $hid;
}
// for each holdings, prepare urls
// GET /almaws/v1/bibs/{mms_id}/holdings/{holding_id}/items
$urllist = array();
$paramurl = '/items?apikey=' . $apikey . '&limit=90';
foreach ($holdinglist as $hid) {
$holdingsurl = '/holdings/' . urlencode($hid);
$urllist["$hid"] = $baseurl . urlencode($mmsid) . $holdingsurl . $paramurl;
}
// call each of the holdings urls at the same time
$results3 = curlMultiRequest($urllist);
// for each holding id, get its items
$items = array();
foreach ($holdinglist as $hid) {
//create xml object based on this holdings xml
$record = new SimpleXMLElement($results3["$hid"]);
$datafields = $record->xpath('/items/item');
// for each item within this holding
foreach ($datafields as $item) {
$count++;
$duedate = "";
$barcode = (string) $item->item_data->barcode;
$items["$barcode"] = array();
$items["$barcode"]["id"] = $barcode;
$items["$barcode"]["barcode"] = $barcode;
$items["$barcode"]["availability"] = (string) $item->item_data->base_status;
if ($items["$barcode"]["availability"]) {
$items["$barcode"]["status"] = "Available";
} else {
$items["$barcode"]["status"] = "Not Available";
}
$items["$barcode"]["location"] = $item->item_data->location['desc'] . " - " . (string) $item->item_data->library['desc'];
$items["$barcode"]["callnumber"] = (string) $item->holding_data->call_number;
$items["$barcode"]["reserve"] = "0";
if (isset($loans["$barcode"])) {
$duedate = (string) $loans["$barcode"];
}
if ($duedate) {
// ISO 8601
$items["$barcode"]["duedate"] = date('l jS F Y', strtotime($duedate));
}
//$items["$barcode"]["requests_placed"] = $item->item_data->requested;
}
}
//print_r($items);
return $items;
}
//call the function
$items = getHolding("9963106302461");
// print out a table
echo "<table border=1><tr><td>id</td><td>availability</td><td>status</td><td>location</td><td>reserve</td><td>callnumber</td><td>duedate</td><td>barcode</td></tr>";
foreach ($items as $oneitem) {
$id = $oneitem['id'];
$status = $oneitem['status'];
$available = $oneitem['availability'];
$location = $oneitem['location'];
$callnumber = $oneitem['callnumber'];
$requested = $oneitem['requested'];
$duedate = $oneitem['duedate'];
//$duedate = "123";
echo "<tr><td>$id</td><td>$available</td><td>$status</td><td>$location</td><td>$requested</td><td>$callnumber</td><td>$duedate</td><td>$id</td></tr>\n";
}
echo "</table>";
echo "<p>return Array: </p><pre>";
print_r($items);
$stop_time = MICROTIME(TRUE);
// get the difference in seconds
$time = $stop_time - $start_time;
PRINT "<p>Elapsed time was $time seconds.";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment