Skip to content

Instantly share code, notes, and snippets.

@chriskeene

chriskeene/Alma.php

Created Sep 2, 2015
Embed
What would you like to do?
Alma get live holdings availability for vufind
<?php
protected 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;
}
//////////////////////////
public function getHolding($id) {
$mmsid = $id;
$apikey = urlencode('xxxxxxxxxxxxxx');
$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 = $this->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
$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 = $this->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) {
$duedate = "";
$barcode = (string) $item->item_data->barcode;
$items["$count"] = array();
$items["$count"]["id"] = $mmsid;
$items["$count"]["barcode"] = $barcode;
$items["$count"]["number"] = $count;
$items["$count"]["item_id"] = $barcode;
$items["$count"]["availability"] = (string) $item->item_data->base_status;
if ($items["$count"]["availability"]) {
$items["$count"]["status"] = "Available";
} else {
$items["$count"]["status"] = "Not Available";
}
$items["$count"]["location"] = $item->item_data->library['desc'] . " - " . (string) $item->item_data->location['desc'];
$items["$count"]["callnumber"] = (string) $item->holding_data->call_number;
$items["$count"]["reserve"] = "0";
if (isset($loans["$barcode"])) {
$duedate = (string) $loans["$barcode"];
}
if ($duedate) {
// ISO 8601
$items["$count"]["duedate"] = date('l jS F Y', strtotime($duedate));
}
$count++;
}
}
return $items;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment