Sierra REST API

Sierra REST API

Some info on working with the Innovative Sierra REST API

 

 

The Innovative PatronAPI allows communication between systems (library in my case). I managed to find a resource on github by aadl which helped a lot - see below.

get_patronapi_data($id)- This function can be passed either a library card number or a patron pnum. It returns an array of data from the III server.

check_validation($id, $pin)- This function takes either a card number or a pnum and a password/pin. It will return an array of information as detailed in the III documentation. Essentially, it will tell you if the combo is good, bad, or if the record does not exist.

The code;

/**
* This is a set of functions that allow access to the III Patron API
* This file is released under the GNU Public License
* @package PatronAPI
* @Author John Blyberg
*/

/**
* Change this to your PatronAPI server
*/
define("APISERVER", "http://your.iii.server.org:4500");

// Nothing below here needs to be configured

/**
* Returns patron data from the API in an easy-to-use array
* @param string $id Can be either a barcode number or a pnum, the function can tell which
* @return array
*/
function get_patronapi_data($id) {

if (strlen($id) < 14) { $id = ".p" . $id; }
$apiurl = APISERVER . "/PATRONAPI/$id/dump";

$api_contents = get_api_contents($apiurl);

$api_array_lines = explode("\n", $api_contents);
while (strlen($api_data[PBARCODE]) != 14 && !$api_data[ERRNUM]) {
foreach ($api_array_lines as $api_line) {
$api_line = str_replace("p=", "peq", $api_line);
$api_line_arr = explode("=", $api_line);
$regex_match = array("/\[(.*?)\]/","/\s/","/#/");
$regex_replace = array('','','NUM');
$key = trim(preg_replace($regex_match, $regex_replace, $api_line_arr[0]));
$api_data[$key] = trim($api_line_arr[1]);
}
}
return $api_data;
}

/**
* Checks tha validity of an id/pin combo
* @param string $id Can be either a barcode number or a pnum, the function can tell which
* @param string $pin The password/pin to use with $id
* @return array
*/
function check_validation($id, $pin) {

$pin = urlencode($pin);
if (strlen($id) < 14) { $id = ".p" . $id; }
$apiurl = APISERVER . "/PATRONAPI/$id/$pin/pintest";

$api_contents = get_api_contents($apiurl);

$api_array_lines = explode("\n", $api_contents);
foreach ($api_array_lines as $api_line) {
$api_line_arr = explode("=", $api_line);
$api_data[$api_line_arr[0]] = $api_line_arr[1];
}

return $api_data;
}

/**
* An internal function to grab the API XML
* @param string $apiurl The formulated url to the patron API record
* @return string
*/
function get_api_contents($apiurl) {
$api_contents = file_get_contents($apiurl);
$api_contents = trim(strip_tags($api_contents));
return $api_contents;
}

?>

I had to modify the above code slightly for my requirements;

function get_patronapi_data($id) {
$apiurl = APISERVER . "/PATRONAPI/$id/dump";
$api_contents = get_api_contents($apiurl);
$api_array_lines = explode("\n", $api_contents);
foreach($api_array_lines as $line){ // go through each item in the array
if(strpos($line, "EXP DATE") !== false){ //if the string "EXP DATE" DOES NOT exist in the string "$line", it will return false
$data = explode("=", $line); //split the string into an array, split by the character "="
return $data[1]; //return the second element in array ($data[0] would be "EXP DATE"
}
}
}

and then include the call;

$data = get_patronapi_data($id);
echo $data;

Hope this helps.

Rate this blog entry:
Virtual Hosts
MySQL Replication Resync

Comments

 
No comments yet