|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace AnarchyService; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class GoogleTranslate |
|
8
|
|
|
*/ |
|
9
|
|
|
class GoogleTranslate |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param $source |
|
14
|
|
|
* @param $target |
|
15
|
|
|
* @param $text |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function translate($source, $target, $text) |
|
19
|
|
|
{ |
|
20
|
|
|
$response = self::requestTranslation($source, $target, $text); |
|
21
|
|
|
return self::getSentencesFromJSON($response); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param $source |
|
26
|
|
|
* @param $target |
|
27
|
|
|
* @param $text |
|
28
|
|
|
* @return bool|string |
|
29
|
|
|
*/ |
|
30
|
|
|
private static function requestTranslation($source, $target, $text) |
|
31
|
|
|
{ |
|
32
|
|
|
$url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e"; |
|
33
|
|
|
$fields = [ |
|
34
|
|
|
'sl' => urlencode($source), |
|
35
|
|
|
'tl' => urlencode($target), |
|
36
|
|
|
'q' => urlencode($text) |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
$fields_string = ""; |
|
40
|
|
|
foreach ($fields as $key => $value) { |
|
41
|
|
|
$fields_string .= $key . '=' . $value . '&'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
rtrim($fields_string, '&'); |
|
45
|
|
|
$ch = curl_init(); |
|
46
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
|
|
47
|
|
|
curl_setopt($ch, CURLOPT_POST, count($fields)); |
|
48
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); |
|
51
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
52
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
53
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1'); |
|
54
|
|
|
|
|
55
|
|
|
$result = curl_exec($ch); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
curl_close($ch); |
|
|
|
|
|
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $json |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private static function getSentencesFromJSON($json) |
|
66
|
|
|
{ |
|
67
|
|
|
$sentencesArray = json_decode($json, true); |
|
68
|
|
|
$sentences = ""; |
|
69
|
|
|
foreach ($sentencesArray["sentences"] as $s) { |
|
70
|
|
|
$sentences .= $s["trans"]; |
|
71
|
|
|
} |
|
72
|
|
|
return $sentences; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|