PHP – Accessing Google Translate with CURL


save the script below to gtproxy.php

change the langpair query string value to your desired value, here I use id (indonesian) to en (english)

langpair=id%7Cen

then test using content as query string to store text which will be translated

eg: gtproxy.php?content=percobaan

<?php

function replace_unicode_escape_sequence($match) {
return mb_convert_encoding(pack(‘H*’, $match[1]), ‘UTF-8’, ‘UCS-2BE’);
}

$query  = trim($_GET[‘content’]);
$query  = urlencode($query);

$ch     = curl_init();
curl_setopt($ch, CURLOPT_URL, “http://www.google.com/uds/Gtranslate?callback=google.language.callbacks.id100&langpair=id%7Cen&key=notsupplied&v=1.0&context=22&q=$query&#8221;);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec ($ch);

curl_close ($ch);

$str = preg_replace_callback(‘/\\\\u([0-9a-f]{4})/i’, ‘replace_unicode_escape_sequence’, $contents);

echo substr($str, 57, -19);

?>

Advertisements