Repository navigation
Expand file tree
/
Copy pathglosbe_api.php
More file actions
81 lines (58 loc) · 2.33 KB
/
Copy pathglosbe_api.php
File metadata and controls
81 lines (58 loc) · 2.33 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
<?php
/**************************************************************
"Learning with Texts" (LWT) is released into the Public Domain.
This applies worldwide.
In case this is not legally possible, any entity is granted the
right to use this work for any purpose, without any conditions,
unless such conditions are required by law.
Developed by J. P. in 2011, 2012, 2013.
***************************************************************/
/**************************************************************
Call: glosbe_api.php?from=...&dest=...&phrase=...
... from=L2 language code (see Glosbe)
... dest=L1 language code (see Glosbe)
... phrase=... word or expression to be translated by
Glosbe API (see http://glosbe.com/a-api)
Call Glosbe Translation API, analyze and present JSON results
for easily filling the "new word form"
***************************************************************/
include "settings.inc.php";
include "connect.inc.php";
include "utilities.inc.php";
$from = trim(stripTheSlashesIfNeeded($_REQUEST["from"]));
$dest = trim(stripTheSlashesIfNeeded($_REQUEST["dest"]));
$phrase = mb_strtolower(trim(stripTheSlashesIfNeeded($_REQUEST["phrase"])), 'UTF-8');
$ok = FALSE;
if ($from != '' && $dest != '' && $phrase != '') {
$glosbe_data = file_get_contents('http://glosbe.com/gapi/translate?from=' . urlencode($from) . '&dest=' . urlencode($dest) . '&format=json&phrase=' . urlencode($phrase));
if(! ($glosbe_data === FALSE)) {
$data = json_decode ($glosbe_data, true);
if ( isset($data['phrase']) ) {
$ok = (($data['phrase'] == $phrase) && (isset($data['tuc'])));
}
}
}
if ( $ok ) {
echo "<h1>" . tohtml($data['phrase']) . "</h1>\n";
if (count($data['tuc']) > 0) {
echo "<p>Translations:</p>\n<ul>\n";
foreach ($data['tuc'] as &$value) {
if (isset($value['phrase'])) {
if (isset($value['phrase']['text']))
echo "<li>" . $value['phrase']['text'] . "</li>\n";
} else if (isset($value['meanings'])) {
if (isset($value['meanings'][0]['text']))
echo "<li>(" . $value['meanings'][0]['text'] . ")</li>\n";
}
}
echo "</ul>\n<hr />\n<pre>\n";
print_r($data['tuc']);
echo "</pre>\n";
} else {
echo "<p>No translations available.</p>";
}
} else {
echo "<h1>" . tohtml($phrase) . "</h1>\n";
echo "<p>No data available or retrieval error./p>";
}
?>