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

一. 翻译接口:

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

有道翻译的源码:需要自己申请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;
    }
}
复制代码

 

三,效果演示

 

英译中

 

中文翻译成英语

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值