来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 要做一个翻译账号,有以下几步:一 有翻译接口:目前有道词典提供了翻译接口,申请地址http: fanyi youdao com openapi?path=data-mode下面是申请介绍:申请完成后,会收到邮件,邮件中有api...
要做一个翻译账号,有以下几步: 一. 有翻译接口: 目前有道词典提供了翻译接口,申请地址 http://fanyi.youdao.com/openapi?path=data-mode 下面是申请介绍: 申请完成后,会收到邮件,邮件中有api key: 数据接口http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=<doctype>&version=1.1&q=要翻译的文本
版本:1.1,请求方式:get,编码方式:utf-8 主要功能:中英互译,同时获得有道翻译结果和有道词典结果(可能没有) 参数说明: type - 返回结果的类型,固定为data doctype - 返回结果的数据格式,xml或json或jsonp version - 版本,当前最新版本为1.1 q - 要翻译的文本,必须是UTF-8编码,字符长度不能超过200个字符,需要进行urlencode编码 errorCode: 0 - 正常 20 - 要翻译的文本过长 30 - 无法进行有效的翻译 40 - 不支持的语言类型 50 - 无效的key
json数据格式举例http://fanyi.youdao.com/openapi.do?keyfrom=<keyfrom>&key= <key>&type=data&doctype=json&version=1.1&q=good { "errorCode":0 "query":"good", "translation":["好"], // 有道翻译 "basic":{ // 有道词典-基本词典 "phonetic":"g?d" "uk-phonetic":"g?d" //英式发音 "us-phonetic":"ɡ?d" //美式发音 "explains":[ "好处", "好的" "好" ] }, "web":[ // 有道词典-网络释义 { "key":"good", "value":["良好","善","美好"] }, {...} ] }
二 程序实现: 有道翻译的源码:需要自己申请key,然后填入
微信调用 define("TOKEN", "qq612312312312"); $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; } } 四,效果演示
中文翻译成英语
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com