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)


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

eg: gtproxy.php?content=percobaan


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, “$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);