Как перевести слово с помощью Google Translate?
Можно так:
function gtranslate($text, $from='auto', $to) {
if ( $from == '' ) $from = 'auto';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://translate.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7"));
curl_setopt($ch, CURLOPT_REFERER, 'http://translate.google.com/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "hl=ru&ie=UTF-8&js=n&prev=_t&layout=2&eotf=1&sl=".$from."&tl=".$to."&text=".urlencode($text));
$res = curl_exec($ch);
if( curl_errno($ch) ) $res = false;
curl_close($ch);
if ($res == false) return;
$_ = '';
if ( preg_match("~<span id=result_box class=\"(short|long)_text\">(.*)</span></span>~", $res, $translate) ) {
$_ = strip_tags($translate[2]);
if ( preg_match('~<~', $text) && preg_match('~>~', $text) ) {
$_ = preg_replace('~<~', '<', $_);
$_ = preg_replace('~>~', '>', $_);
$_ = preg_replace('~</ ~', '</', $_);
}
}
return $_;
}
echo gtranslate('Hello, world!', 'en', 'ru');