求一个能用的调用谷歌翻译api的代码。
网上找了好多,都不能用。

解决方案 »

  1.   


    class Google_API_translator {
    public $out = "";
        function translate($text) {
            $this->out = "";
            $text = urlencode($text);//要翻译的单词
            $google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=en&tl=zh_CN";
            $gphtml = $this->postPage(array("url" => $google_translator_url));
            $this->out = $gphtml;
    return $this->out;
        }
        function postPage($opts) {
            $html = "";if($opts["url"] != "") {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $opts["url"]);            $html = curl_exec($ch);if(curl_errno($ch)) $html = "";
                curl_close ($ch);
            }return $html;
        }
    }
    $g = new Google_API_translator();
    $g->translate("computer");没用过,你捉摸捉摸吧~~反过来,编码要改
      

  2.   

    [[["电脑","computer","Diànnǎo",""]],[["noun",["计算机","电脑"],[["计算机",["computer"]],["电脑",["computer"]]]]],"en",,[["电脑",[5],0,0,1000,0,1,0]],[["computer",4,,,""],["computer",5,[["电脑",1000,0,0],["计算机",0,0,0],["的计算机",0,0,0],["的电脑",0,0,0],["计算机的",0,0,0]],[[0,8]],"computer"]],,,[["en"]],1]2楼,我运行了下,你的代码,出来些这个,我咋看不懂了。咋这么多的数据。
      

  3.   

    嘿嘿~我也没看懂啊,不知道能不能用,你多测两下,hi,look,电脑!
    注:    Translation:
            $20 per 1 M characters of text, where the charges are adjusted in proportion to the number of characters actually provided.
        Language Detection:
            $20 per 1 M characters of text, where the charges are adjusted in proportion to the number of characters actually provided.
      

  4.   

    看url后面的两个东东
    &sl=en&tl=zh_CN
    我敢说,这地方很奇怪的
      

  5.   

    #1 的是抓取google翻译的代码,返回的结果是非标准json(当然js使用它是没问题的)
    数组的
    第一个元素为一数组,依次为 中文词汇、英文词汇、汉语拼音
    第二个元素为一数组,依次为 词性、同义词数组,同义词中英对照数组
    再后面的意义不大,是为生成页面元素而设的你在google翻译页面测试一下就知道了
      

  6.   

    想测试其他语种,怎么不行了,我把zh-CN和en颠倒了一下,结果不对。
      

  7.   

    谷歌翻译不是开始收费了吗?
    Translate This Button 这个玩意儿整页翻译的不错
      

  8.   

    实践出真知,你看看这些地址
    摆弄摆弄选项卡和下拉框,在看看源码
    http://translate.google.cn/?hl=zh-CN&tab=wT#en/zh-CN/
    http://translate.google.cn/?hl=zh-CN&tab=wT#zh-CN/en/
    http://translate.google.cn/?hl=zh-CN&tab=wT#ja/en/
    http://translate.google.cn/?hl=zh-CN&tab=wT#ja/fr/