-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNERD.php
More file actions
202 lines (187 loc) · 6.28 KB
/
Copy pathNERD.php
File metadata and controls
202 lines (187 loc) · 6.28 KB
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
<?php
namespace NERD;
/**
* Interface for APIs at http://nerd.eurecom.fr
*
* @author emanuele
*/
class client {
protected $api_key;
const DOCUMENT_POST = 'http://nerd.eurecom.fr/api/document';
const DOCUMENT_GET = 'http://nerd.eurecom.fr/document';
const ANNOTATION_POST = 'http://nerd.eurecom.fr/api/annotation';
const ENTITY_GET = 'http://nerd.eurecom.fr/api/entity';
/**
* Create a NERD api client.
* Throws exception if built with empty key.
* @param string $key valid Api key
* @throws Exception
*/
public function __construct($key) {
if (empty($key)) {
error_log('Must provide an api key to use NERD services, visit http://nerd.eurecom.fr');
throw new Exception('Missing API key.');
} else {
$this->api_key = $key;
}
}
/**
* Create a document with given text.
* Returns created document's ID or false on failure.
* @param string $text
* @return mixed int if document gets created, false otherwise
*/
public function createDocumentFromString($text) {
if (strlen($text) == 0) {
return false;
} else {
$param = array("text" => $text, "key" => $this->api_key);
$json_encoded = $this->api_request('POST', static::DOCUMENT_POST, $param);
$json = json_decode($json_encoded, true);
if (isset($json['idDocument'])) {
return $json['idDocument'];
} else {
ob_start();
echo "\n[" . date('Y-m-d H:i:s') . "] Error while using nerd API:\n";
var_dump($json);
$log = ob_end_clean();
error_log($log);
return false;
}
}
}
/**
* Create a document from given URI.
* Returns documents ID or false.
* @param string $uri
* @return mixed int on success, false otherwise
*/
public function createDocumentFromUri($uri) {
if (filter_var($uri, FILTER_VALIDATE_URL) !== $uri) {
return false;
} else {
$param = array("uri" => $uri, "key" => $this->api_key);
$json_encoded = $this->api_request('POST', static::DOCUMENT_POST, $param);
$json = json_decode($json_encoded, true);
if (isset($json['idDocument'])) {
return $json['idDocument'];
} else {
ob_start();
echo "\n[" . date('Y-m-d H:i:s') . "] Error while using nerd API:\n";
var_dump($json);
$log = ob_end_clean();
error_log($log);
return false;
}
}
}
/**
* Retrieve a document data, given it's ID.
* @param integer $idDocument Valid document ID
* @return \NERD\schema\Document|boolean Document, if successful, false otherwise.
*/
public function getDocument($idDocument) {
if (!is_numeric($idDocument)) {
return false;
} else {
$json_encoded = $this->api_request('GET', static::DOCUMENT_GET . '/' . $idDocument);
$json = json_decode($json_encoded, true);
$document = new \NERD\schema\Document();
foreach ($json as $docProp => $value) {
$document->$docProp = $value;
}
return $document;
}
}
/**
* Wrapper for CURL requests
* @param type $method
* @param type $url
* @param type $data
* @return boolean
*/
protected function api_request($method, $url, $data = array()) {
$http_code = 0;
switch ($method) {
case 'GET':
case 'get':
$response = $this->curl_get($url . (!empty($data) ? '?' . http_build_query($data) : ''), $http_code);
break;
case 'POST':
case 'post':
$response = $this->curl_post($url, $data, $http_code);
break;
default:
return false;
}
$valid_http_codes = array(200, 201);
if (!in_array($http_code, $valid_http_codes)) {
// Error
ob_start();
echo "\n[" . date('Y-m-d H:i:s') . "] Error while using NERD APIs:\n" . $method . ' ' . $url . "\nParam:\n";
var_dump($data);
echo "\nResponse: $http_code\n";
var_dump($response);
$log = ob_get_clean();
error_log($log);
return $response;
} else {
return $response;
}
}
/**
* Used to perform POST request
* @param type $URL
* @param type $fields
* @param type $http_code
* @return boolean
*/
protected function curl_post($URL, $fields, & $http_code = 0) {
//url-ify the data for the POST
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
//set the url, number of POST vars, POST data
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_POST, !empty($fields));
curl_setopt($c, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Accept-Encoding: gzip, deflate',
));
$contents = curl_exec($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) {
return $contents;
} else {
return FALSE;
}
}
/**
* Used to perform GET request
* @param type $URL
* @param type $http_code
* @return boolean
*/
protected function curl_get($URL, & $http_code = 0) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_TIMEOUT_MS, 0);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Encoding: gzip, deflate'
));
$contents = curl_exec($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) {
return $contents;
} else {
return FALSE;
}
}
}