43 public function __construct($host, $username =
null, $token =
null) {
45 if (is_null($host) || empty($host)) {
49 $this->_host = rtrim($host,
'/');
50 $this->_username = $username;
51 $this->_token = $token;
64 $endpoint = trim($endpoint,
'/');
65 $params = http_build_query($params);
66 $url =
"{$this->_host}/{$endpoint}";
68 return empty($params) ? $url :
"{$url}?{$params}";
79 'email' => $this->_username,
80 'access_token' => $this->_token,
83 return array_merge($params, $credentials);
99 protected function _makeApiRequest($endpoint, $params, $authRequired =
false, $isPost =
false) {
103 if (empty($this->_username) || empty($this->_token)) {
104 throw new Exception(
'Login credentials (username & token) are required for server authentication.');
112 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
115 curl_setopt($ch, CURLOPT_POST, 1);
116 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
125 curl_setopt($ch, CURLOPT_URL, $apiUrl);
126 $response = curl_exec($ch);
130 $error = curl_error($ch);
133 $error =
'HTTP status: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE);
136 throw new Exception($error);
156 public function submitJob($fileName, $fileContent, $citationStyleHash, $metadata =
null) {
159 'fileName' => $fileName,
160 'fileContent' => $fileContent,
161 'citationStyleHash' => $citationStyleHash,
164 if (!is_null($metadata)) {
165 $params[
'fileMetadata'] = json_encode($metadata);
168 $response = $this->
_makeApiRequest(
'api/job/submit', $params,
true,
true);
170 if ($response[
'status'] !=
"success") {
171 throw new Exception(
'Job submission failed.');
174 return $response[
'id'];
194 if ($response[
'status'] !=
"success") {
195 throw new Exception(
'Unable to fetch job status.');
198 return $response[
'jobStatus'];
212 'conversionStage' => self::JOB_CONVERSION_STAGE_ZIP,
229 public function downloadFile($jobId, $filename =
'documents.zip', $destinationDir =
null)
231 if (is_null($destinationDir)) {
232 $destinationDir = sys_get_temp_dir();
236 $filePath = rtrim($destinationDir,
'/') .
'/' . $filename;
238 if (file_exists($filePath)) {
242 if (!@copy($fileUrl, $filePath)) {
243 throw new Exception(
"Unable to copy from {$fileUrl} to {$filePath}");
258 $response = $this->
_makeApiRequest(
'/api/job/citationStyleList', array());
260 if ($response[
'status'] !=
"success") {
261 throw new Exception(
'Job submission failed.');
264 return $response[
'citationStyles'];