Google translator API ( PHP 5 class )


June 5th, 2007 by 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"])."&amp;";
        $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 |

del.icio.us - Digg it - Furl - Google - Netscape - StumbleUpon

13 Responses

  1. Dzamir Says:

    Usefull class. I think that google never released an API because the service is provided “as is”. The translations aren’t very good…

  2. paolo Says:

    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 ) ;)

  3. Increase website traffic with Google language tools | labs @ involutive Says:

    […] Google translator API ( PHP 5 class ) […]

  4. Shahed Says:

    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

  5. paolo Says:

    Yes, of course you can, but only if your hosting allows outgoing connections with curl.

  6. TaLu Says:

    Very nice work! Thanks a lot for share it.
    I will set up a translation service from my site based on your class.

    Thanks.

  7. Chitra Says:

    What are the language pairs available? Can you please give some details on how to use this

  8. asiarlis Says:

    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.

  9. Gevorg Hartyunyan Says:

    Don’t try to translate to Arabic :P

  10. Robert Says:

    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

  11. N Says:

    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”);

  12. Jon Wohl Says:

    Can’t thank you enough, this is exactly what I was looking for!

  13. fiji Says:

    Theres an official Google language API at: http://code.google.com/apis/ajaxlanguage/documentation/

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.