-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleTranslateClient.php
53 lines (38 loc) · 1.91 KB
/
googleTranslateClient.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
<?php
// Unofficial PHP Client for performing Translations using Google Translate.
// Not something novel out here. Just a small piece of hack and its done.
function getTranslatedDataJSONGoogle($url) {
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlSession, CURLOPT_URL, $url);
curl_setopt($curlSession, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36");
$data = curl_exec($curlSession);
$resultCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE);
curl_close($curlSession);
if ($resultCode == 200)
return ($data);
else
return $resultCode;
}
function constructRequestURL($japaneseText) {
$baseURL = "https://translate.google.com/translate_a/single?client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&pc=1&oc=1&otf=1&ssel=0&tsel=0&q=";
$encodedText = urlencode($japaneseText);
$requestURL = $baseURL.$encodedText;
return $requestURL;
}
function extractTitleFromResult($jsonString) {
echo $jsonString."\n"."\n"."\n";
while(strpos($jsonString, ",,") != FALSE) {
$jsonString = str_replace(",,", ",null,", $jsonString);
}
echo $jsonString."\n";
$result = json_decode($jsonString);
return $result[0][0][0]; // I know right, this is how it is!
}
// Example
$japaneseWord = "ハローワールド";
echo "Current word - " . $japaneseWord . "\n";
$requestURLforJSON = constructRequestURL($japaneseWord);
$jsonData = getTranslatedDataJSONGoogle($requestURLforJSON);
$translatedWord = extractTitleFromResult($jsonData);
echo "Translated Word = $translatedWord \n";