forked from MarkFirman/Oxford-Dictionaries-API-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.class.php
More file actions
311 lines (274 loc) · 9.94 KB
/
Copy pathdictionary.class.php
File metadata and controls
311 lines (274 loc) · 9.94 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/***********************************************************************
* # @Author Mark Firman
* # @Project Dictionary API V2
* # @Date 23/05/2019
* # @Email info@markfirman.co.uk
* # @Last Modified 24/05/2019
*/
class Dictionary {
/// API Variables
protected $APP_ID;
protected $APP_KEY;
private $API_URL = "https://od-api.oxforddictionaries.com/api/v2";
public $API_LANG;
/// Data variables
public $json_data;
public $result;
public $selected_result;
/// Class variables
public $definition;
public $shortDefinition;
public $example;
public $phonetic;
public $lexical;
public $audio;
public $origin;
public $language;
/// Error handling
public $errors;
/// Constructor - Intialises the class and its settings
function __construct($_app_id, $_app_key, $_app_lang){
$this->APP_ID = $_app_id;
$this->APP_KEY = $_app_key;
$this->API_LANG = $_app_lang;
}
/// Sends a new word to the dictionary API
public function queryWord($_word){
/* Create HTTP header array */
$options = array(
'http' => array(
'method' => "GET",
'header' => "app_id:".$this->APP_ID."\r\n" .
"app_key:".$this->APP_KEY."\r\n" .
"Content-Type: application/json"
)
);
/* Send query to API */
$context = stream_context_create($options);
$result = @file_get_contents($this->API_URL."/entries/".$this->API_LANG."/".$_word, false, $context);
/* Check the status - set page to error, if response is not 200 */
$status_line = $http_response_header[0];
preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match);
$status = $match[1];
/* Set the word */
$this->word = $_word;
/* Set repsonse for different error messages */
switch($status){
case 200:
$this->errors['status'] = 200;
$this->errors['message'] = "Connected!";
break;
case 400:
/* Bad Request */
$this->errors['status'] = 400;
$this->errors['message'] = "Bad Request";
break;
case 403:
/* Authentication failed */
$this->errors['status'] = 403;
$this->errors['message'] = "Authentication failed";
break;
case 404:
/* Not found */
$this->errors['status'] = 404;
$this->errors['message'] = "Not found";
break;
case 414:
/* Request too long (exceeds 128 characters */
$this->errors['status'] = 414;
$this->errors['message'] = "Request too long";
break;
case 500:
/* Internal server error */
$this->errors['status'] = 500;
$this->errors['message'] = "Internal server error";
break;
case 502:
/* Bad Gateway - API is down */
$this->errors['status'] = 502;
$this->errors['message'] = "Bad Gateway";
break;
case 503:
/* Service unavailable - API is down */
$this->errors['status'] = 503;
$this->errors['message'] = "Service Unavailable";
break;
case 504:
/* Gateway timeout */
$this->errors['status'] = 504;
$this->errors['message'] = "Gateway timeout";
break;
}
/* Store the JSON data */
$this->json_data = json_decode($result);
/* Set the default result set */
$this->selected_result = 0;
/* Force the JSON string to be decoded and stored in class variables */
$this->decode();
/* Return the encoded JSON string so that direct calls can be made */
return json_decode($result);
}
/// Decodes the data
private function decode(){
/* Check the status of the request
* Anything other than 200 is a bad request */
if($this->errors['status'] == 200){
/* Decode the JSON data */
$this->decodeDefinition();
$this->decodeShortDefinition();
$this->decodeExample();
$this->decodePhonetic();
$this->decodeLexical();
$this->decodeAudio();
$this->decodeOrigin();
} else {
/* Show error status and message */
echo "An error occured: ".$this->errors['status']. " - ".$this->errors['message'];
}
}
/// Decodes and stores the definitions
private function decodeDefinition(){
$count = 0;
for($i = 0; $i < 3; $i++){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->definitions[$count])){
$this->definition[$count] = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->definitions[$count];
}
$count++;
}
}
/// Decodes and stores the short definitions
private function decodeShortDefinition(){
$count = 0;
for($i = 0; $i < 3; $i++){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->shortDefinitions[$count])){
$this->shortDefinition[$count] = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->shortDefinitions[$count];
}
$count++;
}
}
//// Decodes and stores the example
private function decodeExample(){
$count = 0;
for($i = 0; $i < 3; $i++){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->examples[0]->text)){
$this->example[$count] = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->senses[0]->examples[0]->text;
}
$count++;
}
}
/// Decodes and stores the phonetic
private function decodePhonetic(){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->pronunciations[0]->phoneticSpelling)){
$this->phonetic = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->pronunciations[0]->phoneticSpelling;
}
}
/// Decodes and stores the lexical entry
private function decodeLexical(){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->lexicalCategory->text)){
$this->lexical = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->lexicalCategory->text;
}
}
/// Decodes and stores the audio URL
private function decodeAudio(){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->pronunciations[0]->audioFile)){
$this->audio = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->pronunciations[0]->audioFile;
}
}
/// Decodes and store the origin
private function decodeOrigin(){
if(isset($this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->etymologies[0])){
$this->origin = $this->json_data->results[$this->selected_result]->lexicalEntries[0]->entries[0]->etymologies[0];
}
}
/// Sets the result set to use
public function setResult($result_set = 0){
if(isset($this->json_data->results[$result_set])){
$this->selected_result = $result_set;
} else {
$this->selected_result = 0;
}
$this->decode();
}
/// Returns the current in-use word
public function getWord(){
return $this->word;
}
/// Returns the definition of the current in-use word
/// Use $count to specify definition to return (some words will have more than 1 definition available)
/// If a definition cannot be found using the provided count, it will default to show the first
public function getDefinition($count = 0){
if(isset($this->definition[$count])){
/* Return the chosen definition */
return $this->definition[$count];
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - a definition for ". $this->word . " cannot be found";
}
}
/// Returns the short definition of the current in-use word
/// Use $count to specify the short definition to return (some words will have more than 1 short definition available)
/// If a short definition cannot be found using the provided count, it will default to show the first
public function getShortDefinition($count = 0){
if(isset($this->shortDefinition[$count])){
/* Return the chosen short definition */
return $this->shortDefinition[$count];
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - a short definition for ". $this->word . " cannot be found";
}
}
/// Returns an example case of the current in-use word
/// Use $count to specify the example to return (some words will have more than 1 example text available)
/// If example text cannot be found using the provided count, it will default to show the first
public function getExample($count = 0){
if(isset($this->example[$count])){
/* Return the chosen short definition */
return $this->example[$count];
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - an example for ". $this->word . " cannot be found";
}
}
/// Returns the phonetic (Finetic for you 'Mericans) of the current in-use word
public function getPhonetic(){
if(isset($this->phonetic)){
return $this->phonetic;
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - the lexical for ". $this->word . " cannot be found";
}
}
/// Returns the lexical category of the current in-use word
public function getLexical(){
if(isset($this->lexical)){
return $this->lexical;
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - the lexical for ". $this->word . " cannot be found";
}
}
/// Returns the URL of the audio pronuciation
public function getAudio(){
if(isset($this->audio)){
return $this->audio;
} else {
/// This code is only reachable if a definition is not found
return "<i>Oops</i> - the audio for ". $this->word . " cannot be found";
}
}
/// Returns the origin of the current in-use word
public function getOrigin(){
if(isset($this->origin)){
return $this->origin;
} else {
/// This code is only reachable if a origin is not found
return "<i>Oops</i> - the origin of ". $this->word . " cannot be found";
}
}
/// Returns the current in-use language
public function getLanguage(){
return $this->API_LANG;
}
}
?>