Google translator API ( PHP 5 class )
paolo This PHP 5 class API is developed on the base of a tool that I wrote some years ago.
Simply works querying Google translation tools with the text and language pair choosen.
I find it useful because of Google haven’t released yet an API for its translation tools.
Here the code!
<?php
class Google_API_translator {
public $opts = array("text" => "", "language_pair" => "en|it");
public $out = "";
function __construct() {
echo "Google Translator API\n(c) 2007 Involutive snc http://www.involutive.com\n";
echo "Author: Paolo Ardoino < paolo@involutive.com >";
}
function setOpts($opts) {
if($opts["text"] != "") $this->opts["text"] = $opts["text"];
if($opts["language_pair"] != "") $this->opts["language_pair"] = $opts["language_pair"];
}
function translate() {
$this->out = "";
$google_translator_url = "http://translate.google.com/translate_t?langpair=".urlencode($this->opts["language_pair"])."&";
$google_translator_data .= "text=".urlencode($this->opts["text"]);
$gphtml = $this->postPage(array("url" => $google_translator_url, "data" => $google_translator_data));
$out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=\"ltr\">"));
$out = substr($out, 29);
$out = substr($out, 0, strpos($out, "</div>"));
$this->out = utf8_encode($out);
return $this->out;
}
// post form data to a given url using curl libs
function postPage($opts) {
$html = "";
if($opts["url"] != "" && $opts["data"] != "") {
$ch = curl_init($opts["url"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $opts["data"]);
$html = curl_exec($ch);
if(curl_errno($ch)) $html = "";
curl_close ($ch);
}
return $html;
}
}
?>
<?php
$g = new Google_API_translator();
$g->setOpts(array("text" => "ciao", "language_pair" => "it|en"));
$g->translate();
echo $g->out;
?>
Download this code: google-translator-api-v1.txt
Author: Paolo Ardoino < paolo@involutive.com >
Posted in Google, search engines and SEO, Web |


June 5th, 2007 at 5:05 pm
Usefull class. I think that google never released an API because the service is provided “as is”. The translations aren’t very good…
June 5th, 2007 at 5:27 pm
I know that translations are not so good, but in next days we’ll post a good idea for exploit Google translations ( is is based on a tool I made some time ago ) ;)
June 8th, 2007 at 9:41 am
[…] Google translator API ( PHP 5 class ) […]
July 13th, 2007 at 5:21 pm
Hello
Thanks for the valuable script.
I have used your script.
It displaying Google translator page,
but is it possible to display the result from my site?
I mean i send request to google for translation and receive the translated value through curl , then display the data to my site?
Hope you can only person who can give the solution.
I am tried to find this solution but failed.
Regards
Shahed
January 30th, 2008 at 2:49 pm
Yes, of course you can, but only if your hosting allows outgoing connections with curl.
February 19th, 2008 at 11:56 pm
Very nice work! Thanks a lot for share it.
I will set up a translation service from my site based on your class.
Thanks.
March 7th, 2008 at 8:31 am
What are the language pairs available? Can you please give some details on how to use this
March 11th, 2008 at 4:00 am
what about greek characters ?
i tried the code and as a response i get
“XX X ± XIX ¬ X ½ XXIO” -> this ..
it seems to be a problem with encoding but i didnt found any sollution
translation is from greek to english
best regards.
March 14th, 2008 at 7:46 pm
Don’t try to translate to Arabic :P
March 20th, 2008 at 5:07 pm
I used an equal approach on translating strings through google.
After several hundred requests i got a HTTP403 error.
It seems like google doesn’t want anymore requests from my server and suspects an atttack.
Does anyone of you know a solution to this Problem?
Thanks,
rob
April 8th, 2008 at 10:27 am
For use throught proxy:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, “http://your.proxy”);
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, “user:pass”);
April 9th, 2008 at 12:53 am
Can’t thank you enough, this is exactly what I was looking for!
June 3rd, 2008 at 6:29 pm
Theres an official Google language API at: http://code.google.com/apis/ajaxlanguage/documentation/