|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Dejurin; |
| 4 | + |
| 5 | +/** |
| 6 | + * GoogleTranslateForFree.php. |
| 7 | + * |
| 8 | + * Class for free use Google Translator. With attempts connecting on failure and array support. |
| 9 | + * |
| 10 | + * @category Translation |
| 11 | + * |
| 12 | + * @author Yuri Darwin |
| 13 | + * @author Yuri Darwin <[email protected]> |
| 14 | + * @copyright 2018 Yuri Darwin |
| 15 | + * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0 |
| 16 | + * |
| 17 | + * @version 1.0.0 |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Main class GoogleTranslateForFree. |
| 22 | + */ |
| 23 | +class GoogleTranslateForFree |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param string $source |
| 27 | + * @param string $target |
| 28 | + * @param string|array $text |
| 29 | + * @param int $attempts |
| 30 | + * |
| 31 | + * @return string|array With the translation of the text in the target language |
| 32 | + */ |
| 33 | + public static function translate($source, $target, $text, $attempts = 5) |
| 34 | + { |
| 35 | + // Request translation |
| 36 | + if (is_array($text)) { |
| 37 | + // Array |
| 38 | + $translation = self::requestTranslationArray($source, $target, $text, $attempts = 5); |
| 39 | + } else { |
| 40 | + // Single |
| 41 | + $translation = self::requestTranslation($source, $target, $text, $attempts = 5); |
| 42 | + } |
| 43 | + |
| 44 | + return $translation; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string $source |
| 49 | + * @param string $target |
| 50 | + * @param array $text |
| 51 | + * @param int $attempts |
| 52 | + * |
| 53 | + * @return array |
| 54 | + */ |
| 55 | + protected static function requestTranslationArray($source, $target, $text, $attempts) |
| 56 | + { |
| 57 | + $arr = array(); |
| 58 | + foreach ($text as $value) { |
| 59 | + // timeout 0.5 sec |
| 60 | + usleep(500000); |
| 61 | + $arr[] = self::requestTranslation($source, $target, $value, $attempts = 5); |
| 62 | + } |
| 63 | + |
| 64 | + return $arr; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string $source |
| 69 | + * @param string $target |
| 70 | + * @param string $text |
| 71 | + * @param int $attempts |
| 72 | + * |
| 73 | + * @return string |
| 74 | + */ |
| 75 | + protected static function requestTranslation($source, $target, $text, $attempts) |
| 76 | + { |
| 77 | + // Google translate URL |
| 78 | + $url = 'https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=uk-RU&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e'; |
| 79 | + |
| 80 | + $fields = array( |
| 81 | + 'sl' => urlencode($source), |
| 82 | + 'tl' => urlencode($target), |
| 83 | + 'q' => urlencode($text), |
| 84 | + ); |
| 85 | + |
| 86 | + if (strlen($fields['q']) >= 5000) { |
| 87 | + throw new \Exception('Maximum number of characters exceeded: 5000'); |
| 88 | + } |
| 89 | + // URL-ify the data for the POST |
| 90 | + $fields_string = self::fieldsString($fields); |
| 91 | + |
| 92 | + $content = self::curlRequest($url, $fields, $fields_string, 0, $attempts); |
| 93 | + |
| 94 | + if (null === $content) { |
| 95 | + //echo $text,' Error',PHP_EOL; |
| 96 | + return ''; |
| 97 | + } else { |
| 98 | + // Parse translation |
| 99 | + return self::getSentencesFromJSON($content); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Dump of the JSON's response in an array. |
| 105 | + * |
| 106 | + * @param string $json |
| 107 | + * |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + protected static function getSentencesFromJSON($json) |
| 111 | + { |
| 112 | + $arr = json_decode($json, true); |
| 113 | + $sentences = ''; |
| 114 | + |
| 115 | + if (isset($arr['sentences'])) { |
| 116 | + foreach ($arr['sentences'] as $s) { |
| 117 | + $sentences .= isset($s['trans']) ? $s['trans'] : ''; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return $sentences; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Curl Request attempts connecting on failure. |
| 126 | + * |
| 127 | + * @param string $url |
| 128 | + * @param array $fields |
| 129 | + * @param string $fields_string |
| 130 | + * @param int $i |
| 131 | + * @param int $attempts |
| 132 | + * |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + protected static function curlRequest($url, $fields, $fields_string, $i, $attempts) |
| 136 | + { |
| 137 | + ++$i; |
| 138 | + $ch = curl_init(); |
| 139 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 140 | + curl_setopt($ch, CURLOPT_POST, count($fields)); |
| 141 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); |
| 142 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 143 | + curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); |
| 144 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 145 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 146 | + //curl_setopt($ch, CURLOPT_HEADER, true); |
| 147 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); |
| 148 | + curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1'); |
| 149 | + |
| 150 | + $result = curl_exec($ch); |
| 151 | + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 152 | + |
| 153 | + if (false === $result || 200 !== $httpcode) { |
| 154 | + // echo $i,'/',$attempts,' Aborted, trying again... ',curl_error($ch),PHP_EOL; |
| 155 | + |
| 156 | + if ($i >= $attempts) { |
| 157 | + //echo 'Could not connect and get data.',PHP_EOL; |
| 158 | + return null; |
| 159 | + //die('Could not connect and get data.'.PHP_EOL); |
| 160 | + } else { |
| 161 | + // timeout 1.5 sec |
| 162 | + usleep(1500000); |
| 163 | + |
| 164 | + return self::curlRequest($url, $fields, $fields_string, $i, $attempts); |
| 165 | + } |
| 166 | + } else { |
| 167 | + return $result; //self::getBodyCurlResponse(); |
| 168 | + } |
| 169 | + curl_close($ch); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Make string with post data fields. |
| 174 | + * |
| 175 | + * @param array $fields |
| 176 | + * |
| 177 | + * @return string |
| 178 | + */ |
| 179 | + protected static function fieldsString($fields) |
| 180 | + { |
| 181 | + $fields_string = ''; |
| 182 | + foreach ($fields as $key => $value) { |
| 183 | + $fields_string .= $key.'='.$value.'&'; |
| 184 | + } |
| 185 | + |
| 186 | + return rtrim($fields_string, '&'); |
| 187 | + } |
| 188 | +} |
0 commit comments