关闭

微信公众平台消息接口开发(3)中英翻译

标签: 微信
839人阅读 评论(0) 收藏 举报
分类:

一. 翻译接口:

目前有很多第三方翻译接口,比如有道词典,百度等,开发人员根据这些接口可自己实现中英翻译。

有道翻译的源码:需要自己申请key,然后填入

复制代码
function translate($keyword)
{
    $url = "http://fanyi.youdao.com/openapi.do?keyfrom=******&key=*******&type=data&doctype=json&version=1.1&q=".urlencode($keyword);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    
    if(curl_errno($ch))
    { echo 'CURL ERROR Code: '.curl_errno($ch).', reason: '.curl_error($ch);}
    
    curl_close($ch);

    //var_dump($content);
    $youdao = json_decode($output, true);
    $errorcode = $youdao['errorCode'];

    $result = "";
    if(isset($errorcode)){
        switch ($errorcode){
            case 0:
                $translation = $youdao['translation'][0]; //网络释义
                $explains = $youdao['basic']['explains']; //基本解释
                if ($translation != "") {
                    $result = $translation;
                }else{
                    foreach ($explains as $value) {
                        $result = $result."\n".$value;
                    }
                }
                break;
            case 20:
                $result = "输入错误:要翻译的文本过长";
                break;
            case 30:
                $result = "系统错误:无法进行有效的翻译";
                break;
            case 40:
                $result = "输入错误:不支持的语言类型";
                break;
            case 50:
                $result = "系统错误:无效的密钥,没有权限翻译";
                break;
            default:
                $result = "系统错误:遇到未知错误,代码:".$errorcode;
                break;
        }
    }
    else{
        $result = "没有获得翻译结果?请和我们联系";
    }
    //return $youdao['translation']['0'];
    return trim($result);
}
复制代码

 

 

二、调用实现

 

复制代码
define("TOKEN", "qq62486");

            
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        logger("R ".$postStr);
        //extract post data
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "text":
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            logger("T ".$resultStr);
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    private function receiveText($object)
    {
        $funcFlag = 0;
        $keyword = trim($object->Content);
        $resultStr = "";
        $cityArray = array();
        $contentStr = "";
        $needArray = false;
        $illegal = false;
        $saytome = false;
        
        if (1 == 1){
            $contentStr = translate($keyword);
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
        //Content 消息内容,大小限制在2048字节,字段为空为不合法请求
        return $resultStr;
    }
    
    private function receiveEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "请直说,我将为你自动翻译";
                break;
            case "unsubscribe":
                $contentStr = "";
                break;
            case "CLICK":
                switch ($object->EventKey)
                {
                    default:
                        $contentStr = "receive a eventkey: ".$object->EventKey;
                        break;
                }
                break;
            default:
                $contentStr = "receive a new event: ".$object->Event;
                break;
        }
        $resultStr = $this->transmitText($object, $contentStr);
        return $resultStr;
    }
    
    private function transmitText($object, $content, $flag = 0)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }
}
复制代码

 

三,效果演示

 

英译中

 

中文翻译成英语

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22356次
    • 积分:329
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:15篇
    • 译文:6篇
    • 评论:2条
    文章分类
    最新评论