Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149219
  • 博文数量: 8
  • 博客积分: 3530
  • 博客等级: 中校
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-31 08:42
文章分类

全部博文(8)

文章存档

2010年(1)

2008年(7)

分类:

2008-03-21 10:53:12

<?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 \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 = "".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, "

"));
        $out = substr($out, 29);
        $out = substr($out, 0, strpos($out, "
"));
        $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;
?>

feedsky
抓虾
pageflakes
Rojo
狗狗
google reader
bloglines
my yahoo
newsgator
netvibes
鲜果
阅读(1202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~