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
|
<?php
/** * This is not an offical use * ref: https://ctrlq.org/code/19909-google-translate-api */
class GoogleTranslateApi{ public function translate($q, $target_language = "zh-HK", $source_language = "en"){ // vdump($this->name_en); $url = 'https://translate.googleapis.com/translate_a/single?' . http_build_query([ 'client' => 'gtx', 'sl' => $source_language, 'tl' => $target_language, 'dt' => 't', 'q' => $q, ]);
$respone = file_get_contents($url);
// $respone = '[[["苹果","apple",,,1]],,"en"]'; $respone = str_replace([',,,',',,'], [',"","","",', ',"","",'], $respone); $result_obj = json_decode($respone); // vdump($url, $respone, $result_obj);
$result = array(); foreach ($result_obj[0] as $key => $r) { if ($r[0]) { $result[] = $r[0]; } } $result = implode(' ', $result); return $result; } }
|