火车采集器软件交流官方论坛

 找回密码
 加入会员
搜索
火车采集器V9版免费下载火车浏览器 - 可视采集,万能群发,全自动脚本工具
查看: 2905|回复: 5

PHP|调用GOOGLE提供的翻译API源码

[复制链接]
发表于 2011-1-17 02:02:54 | 显示全部楼层 |阅读模式
<?php
class Google_API_translator {
        public $url = "http://translate.google.com/translate_t";
        public $text = "";
        public $out = "";

        function setText($text){
            $this->text = $text;
        }

        function translate() {
            $this->out = "";

            $gphtml = $this->postPage($this->url, $this->text);

            $out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=\"ltr\">"));
            $out = substr($out, 29);
            $out = substr($out, 0, strpos($out, "</div>"));

            $this->out = $out;
            return $this->out;
        }

        function postPage($url, $text) {
            $html ='';

            if($url != "" && $text != "") {
                $ch = curl_init($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);
                $fields = array('hl=zh-CN', 'langpair=zh-CN|ko', 'ie=UTF-8','text='.urlencode(mb_convert_encoding($text, 'UTF-8', 'euc-kr')));
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $fields));

                $html = curl_exec($ch);
                if(curl_errno($ch)) $html = "";
                curl_close ($ch);
            }
            return $html;
        }
    }

    //just for test
    $g = new Google_API_translator();
    $g->setText("&#49324;&#46993;&#54644;");
    $g->translate();
    echo $g->out;
?>

文章来源:源码案例基地
发表于 2011-1-17 12:25:22 | 显示全部楼层
不错不错不错不错不错不错不错不错不错不错不错不错
发表于 2011-1-17 12:26:19 | 显示全部楼层
应该不用写个仿远程登陆函数吧
头像被屏蔽
发表于 2011-1-28 15:49:26 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2011-1-28 17:26:21 | 显示全部楼层
回复 4# netloss


    测试OK正常翻译..需要换语言请自己抓包获取语言代码填写到对应的语言列表
头像被屏蔽
发表于 2011-1-28 17:59:06 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

QQ|手机版|Archiver|火车采集器官方站 ( 皖ICP备06000549 )

GMT+8, 2025-7-2 22:46

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表