Heim >Backend-Entwicklung >PHP-Tutorial >微信公众平台开发(六) 翻译功能开发_PHP教程
上一篇文章介绍了微信公众平台天气预报功能的开发,实现了微信公众平台的第一个实际应用,在接下来的这一篇文章中,我们将对微信翻译功能进行简单开发,以供读者参考。
二、思路分析
和上一篇查询天气的思路差不多,首先要对用户发送过来的消息进行判断,判断消息里是否含有“翻译”关键字,如果含有,则提取待翻译内容,然后调用网络上开放的翻译API 进行相关翻译。
网络上有很多翻译API,大家可以根据自己的需求进行选择。这里我们选择应用比较广泛的,翻译功能还比较不错的有道翻译API 和百度翻译API,下面对这两种API的相关信息进行分析。
3.1 有道翻译API
3.1.1 API 地址:http://fanyi.youdao.com/openapi
注意:有道提供的API 接口,在下面的测试时,json 数据格式返回不正确,到网上查阅资料,可以正确翻译的地址为 http://fanyi.youdao.com/fanyiapi,这点注意。
3.1.2 申请key
按照要求填写相关信息,这些信息,下面会使用到,所以请认真如实填写。
申请完之后,会在下方生成API key 和keyfrom,使用API 时会用到。
3.1.3 API 使用范例
3.1.4 数据格式
a. xml 格式
http://fanyi.youdao.com/openapi.do?keyfrom=orchid&key=1008797533&type=data&doctype=&version=1.1&q=这里是有道翻译API
<span <?</span><span xml version="1.0" encoding="UTF-8"</span><span ?></span> <span <</span><span youdao-fanyi</span><span ></span> <span <</span><span errorCode</span><span ></span>0<span </</span><span errorCode</span><span ></span> <span <!--</span><span 有道翻译 </span><span --></span> <span <</span><span query</span><span ></span><span <![CDATA[</span><span 这里是有道翻译API</span><span ]]></span><span </</span><span query</span><span ></span> <span <</span><span translation</span><span ></span> <span <</span><span paragraph</span><span ></span><span <![CDATA[</span><span Here is the youdao translation API</span><span ]]></span><span </</span><span paragraph</span><span ></span> <span </</span><span translation</span><span ></span> <span </</span><span youdao-fanyi</span><span ></span>
http://fanyi.youdao.com/openapi.do?keyfrom=orchid&key=1008797533&type=data&doctype=&version=1.1&q=翻译
<span { "errorCode":0 "query":"翻译", "translation":["translation"], // 有道翻译 "basic":{ // 有道词典-基本词典 "phonetic":"fān yì", "explains":[ "translate", "interpret" ] }, "web":[ // 有道词典-网络释义 { "key":"翻译", "value":["translator","translation","translate","Interpreter"] }, {...} ] }</span>
3.2 百度翻译API
3.2.1 API 地址:http://openapi.baidu.com/public/2.0/bmt/translate
3.2.2 获取api key
开发者在百度连接平台上注册得到的授权API key,详细请参阅:http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%BD%91%E7%AB%99%E6%8E%A5%E5%85%A5/%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97
3.2.3 API 使用范例
百度翻译API 响应的数据格式为UTF-8编码的PHP数组对应的标准JSON字符串。
<span { “from”:”zh”, “to”:”en”, “trans_result”:[] }</span>
trans_result 为一个数组,其中每一个{}就是一个段落,结构如下所示:
<span trans_result: [ {}, {}, {} ]</span>
段落结果即为trans_result 数组中的一项:
<span { “src”:””, “dst”:”” }</span>
段落结果说明:
经json_decode 后的形式:
<span { "from": "en", "to": "zh", "trans_result": [ { "src": "today", "dst": "今天" } ] }</span>
翻译消息的格式是 “翻译+待翻译内容”,所以首先截取前两个字,判断是否为 “翻译” 关键字。
使用php函数 mb_substr() 截取,关于该函数的用法上一篇已经讲过,这里不再赘述。
从消息的开头开始截取,截取两个字符,然后加以判断是否为 “翻译” 关键字。
判断是否只输入“翻译”两字,这样输入,没有待翻译内容,则输入的消息也不正确。
接下来进行待翻译内容提取:
从消息的开头第3个字符开始截取,截取202个字符,截取出来的即为待翻译内容。
接着调用函数进行翻译。
<span //</span><span 调用有道词典</span> <span $contentStr</span> = <span $this</span>->youdaoDic(<span $word</span><span ); </span><span //</span><span 调用百度词典</span> <span $contentStr</span> = <span $this</span>->baiduDic(<span $word</span>);
5.1 有道翻译API
数据接口:
http://fanyi.youdao.com/openapi.do?keyfrom=<span <</span><span keyfrom</span><span ></span><span &key</span>=<span <</span><span key</span><span ></span><span &type</span>=data<span &doctype</span>=<span <</span><span doctype</span><span ></span><span &version</span>=1.1<span &q</span>=要翻译的文本
5.1.1 xml 格式
关键代码如下:
<span public</span> <span function</span> youdaoDic(<span $word</span><span ){ </span><span $keyfrom</span> = "orchid"; <span //</span><span 申请APIKEY 时所填表的网站名称的内容</span> <span $apikey</span> = "YourApiKey"; <span //</span><span 从有道申请的APIKEY //有道翻译-xml格式</span> <span $url_youdao</span> = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.<span $keyfrom</span>.'&key='.<span $apikey</span>.'&type=data&doctype=xml&version=1.1&q='.<span $word</span><span ; </span><span $xmlStyle</span> = <span simplexml_load_file</span>(<span $url_youdao</span><span ); </span><span $errorCode</span> = <span $xmlStyle</span>-><span errorCode; </span><span $paras</span> = <span $xmlStyle</span>->translation-><span paragraph; </span><span if</span>(<span $errorCode</span> == 0<span ){ </span><span return</span> <span $paras</span><span ; }</span><span else</span><span { </span><span return</span> "无法进行有效的翻译"<span ; }<br />}</span>
说明:
$xmlStyle = simplexml_load_file($url_youdao); // PHP 函数,将XML 文档载入对象中。
$errorCode = $xmlStyle->errorCode; // 获取错误码
$paras = $xmlStyle->translation->paragraph; // 获取翻译内容
5.1.2 json 格式
关键代码如下:
<span public</span> <span function</span> youdaoDic(<span $word</span><span ){ </span><span $keyfrom</span> = "orchid"; <span //</span><span 申请APIKEY时所填表的网站名称的内容</span> <span $apikey</span> = "YourApiKey"; <span //</span><span 从有道申请的APIKEY //有道翻译-json格式</span> <span $url_youdao</span> = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.<span $keyfrom</span>.'&key='.<span $apikey</span>.'&type=data&doctype=json&version=1.1&q='.<span $word</span><span ; </span><span $jsonStyle</span> = <span file_get_contents</span>(<span $url_youdao</span><span ); </span><span $result</span> = json_decode(<span $jsonStyle</span>,<span true</span><span ); </span><span $errorCode</span> = <span $result</span>['errorCode'<span ]; </span><span $trans</span> = ''<span ; </span><span if</span>(<span isset</span>(<span $errorCode</span><span )){ </span><span switch</span> (<span $errorCode</span><span ){ </span><span case</span> 0: <span $trans</span> = <span $result</span>['translation']['0'<span ]; </span><span break</span><span ; </span><span case</span> 20: <span $trans</span> = '要翻译的文本过长'<span ; </span><span break</span><span ; </span><span case</span> 30: <span $trans</span> = '无法进行有效的翻译'<span ; </span><span break</span><span ; </span><span case</span> 40: <span $trans</span> = '不支持的语言类型'<span ; </span><span break</span><span ; </span><span case</span> 50: <span $trans</span> = '无效的key'<span ; </span><span break</span><span ; </span><span default</span>: <span $trans</span> = '出现异常'<span ; </span><span break</span><span ; } } </span><span return</span> <span $trans</span><span ; }</span>
说明:
把整个文件读入一个字符串中
$result = json_decode($jsonStyle,true); // 对JSON 格式的字符串进行编码
$errorCode = $result['errorCode']; // 获取错误码
$trans = $result['translation']['0']; // 获取翻译结果
5.2 百度翻译API
百度翻译API提供UTF-8编码的PHP数组对应的标准JSON字符串,而且提供了 中->英,中->日,英->中,日->中 四种互译,比有道翻译多了一种。
关键代码如下:
<span //</span><span 百度翻译</span> <span public</span> <span function</span> baiduDic(<span $word</span>,<span $from</span>="auto",<span $to</span>="auto"<span ){ </span><span //</span><span 首先对要翻译的文字进行 urlencode 处理</span> <span $word_code</span>=<span urlencode</span>(<span $word</span><span ); </span><span //</span><span 注册的API Key</span> <span $appid</span>="<span YourApiKey</span>"<span ; </span><span //</span><span 生成翻译API的URL GET地址</span> <span $baidu_url</span> = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".<span $appid</span>."&q=".<span $word_code</span>."&from=".<span $from</span>."&to=".<span $to</span><span ; </span><span $text</span>=json_decode(<span $this</span>->language_text(<span $baidu_url</span><span )); </span><span $text</span> = <span $text</span>-><span trans_result; </span><span return</span> <span $text</span>[0]-><span dst; } </span><span //</span><span 百度翻译-获取目标URL所打印的内容</span> <span public</span> <span function</span> language_text(<span $url</span><span ){ </span><span if</span>(!<span function_exists</span>('file_get_contents'<span )){ </span><span $file_contents</span> = <span file_get_contents</span>(<span $url</span><span ); }</span><span else</span><span { </span><span //</span><span 初始化一个cURL对象</span> <span $ch</span> =<span curl_init(); </span><span $timeout</span> = 5<span ; </span><span //</span><span 设置需要抓取的URL</span> curl_setopt (<span $ch</span>, CURLOPT_URL, <span $url</span><span ); </span><span //</span><span 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上</span> curl_setopt (<span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span ); </span><span //</span><span 在发起连接前等待的时间,如果设置为0,则无限等待</span> curl_setopt (<span $ch</span>, CURLOPT_CONNECTTIMEOUT, <span $timeout</span><span ); </span><span //</span><span 运行cURL,请求网页</span> <span $file_contents</span> = curl_exec(<span $ch</span><span ); </span><span //</span><span 关闭URL请求</span> curl_close(<span $ch</span><span ); } </span><span return</span> <span $file_contents</span><span ; }</span>
说明:
baiduDic() 函数:
language_text($baidu_url)); // 调用language_text() 函数获取目标URL所打印的内容,然后对JSON 格式的字符串进行编码
trans_result; //获取翻译结果数组
dst; //取第一个数组的dst 结果。
language_text() 函数:
有道翻译-xml 格式:
有道翻译-json 格式:
百度翻译:
注意:该翻译功能放在SAE上能够正常运行,但在BAE上运行不成功,各位有兴趣自行测试一下。
请到QQ群213260412共享里下载使用。
请关注 卓锦苏州 微信公众帐号,卓锦苏州 基于SAE 平台开发,针对于主流的微信功能进行开发测试。
您可以关注 卓锦苏州 公众帐号进行功能测试,以及获取新的应用开发。
1. 登录微信客户端,朋友们 -> 添加朋友 -> 搜号码 -> zhuojinsz,查找并关注。
2. 扫描二维码:
卓锦苏州功能列表。
We Believe, Great People Share Knowledge...