Skip to content

Instantly share code, notes, and snippets.

@SimonXIX
Last active May 15, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimonXIX/f963bd67af5c5385722d to your computer and use it in GitHub Desktop.
Save SimonXIX/f963bd67af5c5385722d to your computer and use it in GitHub Desktop.
Script to display real-time reading lists information from Ex Libris Alma / Leganto. This was a proof-of-concept of API integration for the Imperial College Business School's eventual integration with WordPress.
<?php
# @name: reading_lists.php
# @version: 0.1
# @license: GNU General Public License version 3 (GPLv3) <https://www.gnu.org/licenses/gpl-3.0.en.html>
# @purpose: Display real-time reading lists information for Imperial College Library
# @author: Simon Barron <s.barron@imperial.ac.uk>
# @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="reading_lists.css">
<title>Reading lists - Imperial College London</title>
</head>
<body>
<table>
<tr>
<th>List ID</th>
<th>Reading list name</th>
<th>Title</th>
<th>Author</th>
<th>ISBN/ISSN</th>
</tr>
<?php
$course001 = urlencode('xxxxxxxxxxxxxx');
$coursearray = [$course001];
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 $coursearray;
$apikey = urlencode('xxxxxxxxxxxxxx');
$baseurl = 'https://api-eu.hosted.exlibrisgroup.com/almaws/v1/courses/';
$format = 'xml';
$count=0;
$suffixurl = '/reading-lists' . '?apikey=' . $apikey . '&format=' . $format;
foreach ($coursearray as $key => $value){
$urllist['readinglists'][$key] = $baseurl . $value . $suffixurl;
}
$readinglists = curlMultiRequest($urllist['readinglists']);
foreach ($readinglists as $key => $value){
$xml = new SimpleXMLElement($value);
$items[$key]['reading_list_id'] = $xml->reading_list->id;
$items[$key]['reading_list_name'] = $xml->reading_list->name;
}
$suffixurl = '/citations' . '?apikey=' . $apikey . '&format=' . $format;
foreach ($items as $key => $value){
$itemurls[$key] = $baseurl . $coursearray[$key] . '/reading-lists/' . urlencode($value['reading_list_id']) . $suffixurl;
}
$itemsxml = curlMultiRequest($itemurls);
foreach ($itemsxml as $key => $value){
$itemxml = new SimpleXMLElement($value);
$item = $itemxml->xpath('/citations/citation');
foreach($item as $key => $value){
$items[$key]['reading_list_id'] = $items[0]['reading_list_id'];
$items[$key]['reading_list_name'] = $items[0]['reading_list_name'];
$items[$key]['title'] = (string) $value->metadata->title;
$items[$key]['author'] = (string) $value->metadata->author;
$items[$key]['isbn'] = (string) $value->metadata->isbn;
print
"<tr><td>" . $items[$key]['reading_list_id'] . "</td><td>" . $items[$key]['reading_list_name'] . "</td><td>" . $items[$key]['title'] . "</td><td>" . $items[$key]['author'] . "</td><td>" . $items[$key]['isbn'] . "</td></tr>";
}
}
}
getItems();
?>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment