Tech Blog

Calling Alma APIs using PHP

The API Console or the ‘Try it now!‘ button (included in each API documentation) has a nice feature which you don’t want to miss out:
After pressing ‘Execute’, the ‘Response‘ tab will be displayed. But if you click on the ‘Query‘ tab you can use the drop-down on the right to select one of many programming languages, and code in that language will be generated for your specific API needs.

A useful “language” is curl which produces a one-line command that you can use on any Unix server to test the API.

Among other languages, we have PHP, for which the sample code can be expanded as in the below sample.
If you are an experienced PHP developer the below will be obvious, but for beginners it might be useful. enjoy!

p.s. Don’t forget to enable curl in php.ini

extension=php_curl.dll

$ch = curl_init();

$baseUrl = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/{user_id}';

$templateParamNames = array('{user_id}');
$templateParamValues = array(urlencode('exl_impl'));
$baseUrl = str_replace($templateParamNames, $templateParamValues, $baseUrl);

$queryParams = array(
    'user_id_type' => 'all_unique',
    'view' => 'full',
    'expand' => 'none',
    'apikey' => 'your-API-key'
);

$url = $baseUrl . "?" . http_build_query($queryParams);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
curl_close($ch);

//echo $response;
var_dump($response);

 

Leave a Reply