-
Notifications
You must be signed in to change notification settings - Fork 10
/
LibLouis-Remoter.php
127 lines (100 loc) · 3.46 KB
/
LibLouis-Remoter.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
<?php
/*
LibLouis-Remoter is an open source project developed by the University of
Maryland Institute for Technology in the Humanities.
PHP-LibLouis, LibLouis-Remoter, and Remote-LibLouis was written by Cory
Bohon (@coryb).
View README.md for information on how to use this library.
Licensed under the MIT Open Source License. The text for this license
is available at http://opensource.org/licenses/MIT.
*/
function returnBrailleForString($textToTranslate, $url)
{
$content = json_encode(array("content" => $textToTranslate));
# Use the WordPress function if it's available - otherwise, fall back to
# using libcurl.
if(function_exists('wp_remote_post')) {
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 15,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array("Content-type: application/json"),
'body' => $content,
'cookies' => array()
)
);
if( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
die("Error: call to URL $url failed with $error_message.");
} else {
$json_response = $response["body"];
}
}
else {
# cURL implementation to handle posting the content and
# getting a response back
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die(
"Error: call to URL $url failed with status $status, " .
"response $json_response, curl_error " .
curl_error($curl) . ", curl_errno " . curl_errno($curl)
);
}
curl_close($curl);
}
$response = json_decode($json_response, true);
return $response['content'];
}
function returnBRFFileForString($textToTranslate, $url)
{
$content = json_encode(array("content" => $textToTranslate));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die(
"Error: call to URL $url failed with status $status, " .
"response $json_response, curl_error " .
curl_error($curl) . ", curl_errno " . curl_errno($curl)
);
}
curl_close($curl);
$response = json_decode($json_response, true);
# create temporary filename
$_translatedText = tempnam(sys_get_temp_dir(), "pll_");
if($_translatedText == FALSE)
{
return "";
}
else
{
# Write the contents of the passed text to the temporary file
$_handle = fopen($_translatedText, "w");
fwrite($_handle, $response);
fclose($_handle);
}
# return the filename
return $_translatedText;
}
?>