Function Google Translate by PHP

Share google translate code by PHP

Các thảo luận về việc sử dụng Google Translate mình thấy rất nhiều. Nhưng anh Sincos chia sẻ cái mà ảnh sử dụng thì mình note lại luôn để khi nào có dịp thì tham khảo nhanh hoặc chữa cháy. Nếu bạn quan tâm thì xem và thảo luận cùng mình.

function google_translate($st, $to_language = ‘en’, $from_language = ‘auto’)
{
$google_translate_end_point_url = ‘https://translate.google.com/translate_a/single?client=it&dt=t&dt=rmt&dt=bd&dt=rms&dt=qca&dt=ss&dt=md&dt=ld&dt=ex&otf=2&dj=1&q=’.urlencode($st).’&hl=vi&ie=UTF-8&oe=UTF-8&sl=’.$from_language.’&tl=’.$to_language;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $google_translate_end_point_url);

$user_agent = ‘GoogleTranslate/5.25.59107 (iPhone; iOS 12.1; vi; iPhone8,4)’;
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$result = curl_exec($ch);

curl_close($ch);

$obj = @json_decode($result);
if(!$obj)
{
echo $result;
exit();
}
return isset($obj->sentences[0]->trans) ? $obj->sentences[0]->trans : false;
}

By Sincos


function requestTranslation($source, $target,$proxy,$text){
// Google translate URL
$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”;
$fields = array(
‘sl’ => urlencode($source),
‘tl’ => urlencode($target),
‘q’ => urlencode($text)
);

// URL-ify the data for the POST
$fields_string = “”;
foreach ($fields as $key => $value) {
$fields_string .= $key . ‘=’ . $value . ‘&’;
}

rtrim($fields_string, ‘&’);

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, ‘UTF-8’);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, ‘AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1’);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// Execute post
$result = curl_exec($ch);
curl_close($ch);

return $result;
}

By Dung Vuong


Python by thitgaluoc

import requests

def translate_google_ios(text, from, to):
iosheader = {
“User-Agent”: “iOSTranslate”,
“Content-Type”: “application/x-www-form-urlencoded”
}
iosep = “http://translate.google.cn/translate_a/single”

iosparams = {
“dt”: “t”,
“q”: text,
“tl”: from,
“ie”: “UTF-8”,
“sl”: to,
“client”: “ia”,
“dj”: “1”
}
return requests.post(iosep, params=iosparams, headers=iosheader)

def translate_google_android(text, _from, _to):
headers = {
“User-Agent”: “AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1”}
ep = “https://translate.google.com/translate_a/single”

params = {
“dt”: “t”,
“q”: text,
“tl”: _to,
“ie”: “UTF-8”,
“oe”: “UTF-8”,
“sl”: _from,
“client”: “at”,
“dj”: “1”,
“hl”: “es-ES”,
“inputm”: “2”,
“otf”: “2”,
“iid”: “1dd3b944-fa62-4b55-b330-74909a99969e”,
}
return requests.post(ep, params=params, headers=headers)


By automan

function requestTranslation($source, $target,$proxy,$text){
// Google translate URL
$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”;
$fields = array(
‘sl’ => urlencode($source),
‘tl’ => urlencode($target),
‘q’ => urlencode($text)
);

// URL-ify the data for the POST
$fields_string = “”;
foreach ($fields as $key => $value) {
$fields_string .= $key . ‘=’ . $value . ‘&’;
}

rtrim($fields_string, ‘&’);

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, ‘UTF-8’);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, ‘AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1’);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// Execute post
$result = curl_exec($ch);
curl_close($ch);

return $result;

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Website này sử dụng Akismet để hạn chế spam. Tìm hiểu bình luận của bạn được duyệt như thế nào.