-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sierra.php
206 lines (182 loc) · 6.13 KB
/
Sierra.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* @file Sierra.php
*
* Sierra API class to make calls to the Sierra REST API
*
*
* Example Usage:
*
* The example gets information on bib ID 3996024 and limits the results to 20 records only
* including the fields id, location, and status.
*
* include('Sierra.php');
*
* $s = new Sierra(array(
* 'endpoint' => 'Sierra REST API Endpoint (ie https://lib.example.edu/iii/sierra-api/v1/)',
* 'key' => 'Sierra Client Key',
* 'secret' => 'Sierra Client Secret'
* 'tokenFile' => 'Location to the temp file to keep token infomation, default: /tmp/SierraToken'
* ));
*
* $bibInformation = $s->query('items', array(
* 'bibIds' => '3996024',
* 'limit' => '20',
* 'fields' => 'id,location,status'
* ));
*
*
*
* @author Sean Watkins <slwatkins@uh.edu>
* @copyright 2014 Sean Watkins
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Sierra {
/**
* The Authorization Token array returned when accessing a token request.
*
* @var array
*/
public $token = array();
/**
* The Sierra configuration
*
* @var array
*/
public $config = array(
'tokenFile' => '/tmp/SierraToken'
);
/**
* Constructor
*
* @param array $config Array of configuration information for Sierra
*/
public function __construct($config) {
$this->config = array_merge($this->config, $config);
}
/**
* Makes the resource request
*
* @param string $resource The resource being requested
* @param array $params Array of paramaters
* @param boolean $marc True to have the response include MARC data
* @return array Array of data
*/
public function query($resource, $params = array(), $marc = false) {
if (!$this->_checkToken()) return null;
$headers = array('Authorization: ' . $this->token['token_type'] . ' ' . $this->token['access_token']);
if ($marc) {
$headers[] = 'Accept: application/marc-in-json';
}
$response = $this->_request($this->config['endpoint'] . $resource, $params, $headers);
if ($response['status'] != 200) return null;
return json_decode($response['body'], true);
}
/**
* Checks if Authentication Token exists or has expired. A new Authentication Token will
* be created if one does not exist.
*
* @return boolean True if token is valid
*/
private function _checkToken() {
if (file_exists($this->config['tokenFile'])) {
$this->token = json_decode(file_get_contents($this->config['tokenFile']), true);
}
if (!$this->token || (time() >= $this->token['expires_at'])) {
return $this->_accessToken();
}
return true;
}
/**
* Requests a Authentication Token from Sierra
*
* @return boolean True if a token is created
*/
private function _accessToken() {
$auth = base64_encode($this->config['key'] . ':' . $this->config['secret']);
$response = $this->_request($this->config['endpoint'] . 'token', array('grant_type', 'client_credentials'), array('Authorization: Basic ' . $auth), 'post');
$token = json_decode($response['body'], true);
if (!$token) return false;
if (!isset($token['error'])) {
$token['expires_at'] = time() + $token['expires_in'];
$this->token = $token;
file_put_contents($this->config['tokenFile'], json_encode($token));
return true;
}
return false;
}
/**
* Requests data from Sierra
*
* @param string $url The full URL to the REST API call
* @param array $params The query paramaters to pass to the call
* @param array $header Additional header information to include
* @param string $type The request type 'GET' or 'POST'
* @return array Result array
*
* ### Result keys returned
* - 'status': The return status from the server
* - 'header': The header information fo the server
* - 'body': The body of the message
*/
private function _request($url, $params = array(), $header = array(), $type = 'get') {
$type = strtolower($type);
$s = curl_init();
if ($type == 'post') {
$header[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($params));
}
else {
$url .= ($params ? '?' . http_build_query($params) : '');
}
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_TIMEOUT, 60);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($s, CURLOPT_USERAGENT, 'Sierra PHP Test/0.1');
curl_setopt($s, CURLOPT_HEADER, true);
if ($header) {
curl_setopt($s, CURLOPT_HTTPHEADER, $header);
}
$result = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($s, CURLINFO_HEADER_SIZE);
$header = $this->_parseResponseHeaders(substr($result, 0, $headerSize));
$body = substr($result, $headerSize);
$response = array(
'status' => $status,
'header' => $header,
'body' => $body
);
curl_close($s);
return $response;
}
/**
* Parse response headers into a array
*
* @param string $header The header information as a string
* @return array
*/
private function _parseResponseHeaders($header) {
$headers = array();
$h = explode("\r\n", $header);
foreach ($h as $header) {
if (strpos($header, ':') !== false) {
list($type, $value) = explode(":", $header, 2);
if (isset($headers[$type])) {
if (is_array($headers[$type])) {
$headers[$type][] = trim($value);
}
else {
$headers[$type] = array($headers[$type], trim($value));
}
}
else {
$headers[$type] = trim($value);
}
}
}
return $headers;
}
}