Jun 20

You may find this useful to translate some part of your site content. ;)

Usage:

$google = new Google_API_translator();
$google->setOpts($text_info);
$google->translate();
echo $j->out;

<?php
class Google_API_translator {
    public $opts = array("text" => "", "language_pair" => "en|tl");
    public $out = "";

    function __construct() {
    }

    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;
    }
}
?>
Tagged with:
Mar 25

Want to crawl websites like google bot or any other robot? Well, here it is

$url="http://blog.bauson.com";

$curl=curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (http://www.googlebot.com/bot.html)');
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,2);
$buffer = curl_exec($curl);
$html = @mb_convert_encoding($buffer, 'HTML-ENTITIES', 'utf-8');
curl_close($curl);
if (empty($html)){
    print "Nothing to display";
}
else{
    print $html;
}
?>

You might want to store google bot IPs after reading this. :-)

Tagged with:
preload preload preload
Bless CV