微信公众平台开发(六) 翻译功能开发
来源:http://www.cnblogs.com/mchina/p/3170565.html<p></p><div><br></div><div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">一、简介</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">上一篇文章介绍了<a title="微信公众平台天气预报功能开发" href="http://www.cnblogs.com/mchina/p/3170551.html" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">微信公众平台天气预报</a>功能的开发,实现了微信公众平台的第一个实际应用,在接下来的这一篇文章中,我们将对微信翻译功能进行简单开发,以供读者参考。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0); font-size: 16px;"><strong>二、思路分析</strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">和上一篇查询天气的思路差不多,首先要对用户发送过来的消息进行判断,判断消息里是否含有“翻译”关键字,如果含有,则提取待翻译内容,然后调用网络上</span><span style="line-height: 1.8;">开放的翻译API 进行相关翻译。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">三、翻译API 分析</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">网络上有很多翻译API,大家可以根据自己的需求进行选择。这里我们选择应用比较广泛的,翻译功能还比较不错的有道翻译API 和百度翻译API,下面对这两种API的相关信息进行分析。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.1 有道翻译API</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.1.1 API 地址:<a href="http://fanyi.youdao.com/openapi" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://fanyi.youdao.com/openapi</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>注意:</strong>有道提供的API 接口,在下面的测试时,json 数据格式返回不正确,到网上查阅资料,可以正确翻译的地址为 <a href="http://fanyi.youdao.com/fanyiapi" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://fanyi.youdao.com/fanyiapi</a>,这点注意。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.1.2 申请key</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">按照要求填写相关信息,这些信息,下面会使用到,所以请认真如实填写。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/26234717-75763fbce1a246b7a7efed09c6df8c9a.jpg" alt="" width="980" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">申请完之后,会在下方生成API key 和keyfrom,使用API 时会用到。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/26234736-e6197576ceea452b966cee0927a204ca.jpg" alt="" width="980" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.1.3 API 使用范例</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/26234956-7cbdb2f3f3394ceaa5b154be00400f35.jpg" alt="" width="980" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.1.4 数据格式</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">a. xml 格式</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span class="host" style="line-height: 1.8;">http://fanyi.youdao.com/openapi.do?</span><span class="path" style="line-height: 1.8;">keyfrom=<span class="realKeyfrom" style="line-height: 1.8;">orchid</span>&key=<span class="realKey" style="line-height: 1.8;">1008797533</span></span><span class="para" style="line-height: 1.8;">&type=data&doctype=<span style="line-height: 1.8; color: rgb(255, 0, 0);">xml</span>&version=1.1</span><span class="query" style="line-height: 1.8;">&q=这里是有道翻译API</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">xml version="1.0" encoding="UTF-8"</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">youdao-fanyi</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">errorCode</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>0<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">errorCode</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 128, 0);"><!--</span><span style="line-height: 1.8; color: rgb(0, 128, 0);"> 有道翻译 </span><span style="line-height: 1.8; color: rgb(0, 128, 0);">--></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">query</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><!]></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">query</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">translation</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">paragraph</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><!]></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">paragraph</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">translation</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">youdao-fanyi</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">b. json 格式</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;"><span class="host" style="line-height: 1.8;">http://fanyi.youdao.com/openapi.do?</span><span class="path" style="line-height: 1.8;">keyfrom=<span class="realKeyfrom" style="line-height: 1.8;">orchid</span>&key=<span class="realKey" style="line-height: 1.8;">1008797533</span></span><span class="para" style="line-height: 1.8;">&type=data&doctype=<span style="line-height: 1.8; color: rgb(255, 0, 0);">json</span>&version=1.1</span><span class="query" style="line-height: 1.8;">&q=翻译</span></span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">{
"errorCode":0
"query":"翻译",
"translation":["translation"], // 有道翻译
"basic":{ // 有道词典-基本词典
"phonetic":"fān yì",
"explains":[
"translate",
"interpret"
]
},
"web":[ // 有道词典-网络释义
{
"key":"翻译",
"value":["translator","translation","translate","Interpreter"]
},
{...}
]
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.2 百度翻译API</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.2.1 API 地址:<a href="http://openapi.baidu.com/public/2.0/bmt/translate" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://openapi.baidu.com/public/2.0/bmt/translate</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">3.2.2 获取api key</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">开发者在百度连接平台上注册得到的授权API key,详细请参阅:<a href="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" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">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</a></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/27000538-acf99e9ab04f444891b8468f80b0bf90.jpg" alt="" width="980" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">3.2.3 API 使用范例</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;"><img src="http://images.cnitblog.com/blog/370046/201307/27001029-773a66ab4c474d18abcecde9a7ae3bfd.jpg" alt="" style="border: 0px; max-width: 900px;"></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">3.2.4 数据格式</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">百度翻译API 响应的数据格式为UTF-8编码的PHP数组对应的标准JSON字符串。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">{
“from”:”zh”,
“to”:”en”,
“trans_result”:[]
}</span></pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">trans_result 为一个数组,其中每一个{}就是一个段落,结构如下所示:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">trans_result: [
{},
{},
{}
]</span></pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">段落结果即为trans_result 数组中的一项:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">{
“src”:””,
“dst”:””
}</span></pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">段落结果说明:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/27001736-300dbb3959a4472aac8a878bcc671f3c.jpg" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">经json_decode 后的形式:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">{
"from": "en",
"to": "zh",
"trans_result": [
{
"src": "today",
"dst": "今天"
}
]
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">四、关键字判断与待翻译内容读取</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">翻译消息的格式是 “翻译+待翻译内容”,所以首先截取前两个字,判断是否为 “翻译” 关键字。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">使用php函数 mb_substr() 截取,关于该函数的用法<a href="http://www.cnblogs.com/mchina/p/3170551.html" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">上一篇</a>已经讲过,这里不再赘述。</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$str_trans = mb_substr($keyword,0,2,"UTF-8");</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">从消息的开头开始截取,截取两个字符,然后加以判断是否为 “翻译” 关键字。</span><br></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$str_valid = mb_substr($keyword,0,-2,"UTF-8");</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">判断是否只输入“翻译”两字,这样输入,没有待翻译内容,则输入的消息也不正确。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">接下来进行待翻译内容提取:</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$word = mb_substr($keyword,2,220,"UTF-8");</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">从消息的开头第3个字符开始截取,截取202个字符,截取出来的即为待翻译内容。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">接着调用函数进行翻译。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">调用有道词典</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$contentStr</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$this</span>->youdaoDic(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">调用百度词典</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$contentStr</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$this</span>->baiduDic(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span>);</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">五、具体实现</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.1 有道翻译API</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">数据接口:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;">http://fanyi.youdao.com/openapi.do?keyfrom=<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">keyfrom</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(255, 0, 0);">&key</span>=<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">key</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(255, 0, 0);">&type</span>=data<span style="line-height: 1.8; color: rgb(255, 0, 0);">&doctype</span>=<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">doctype</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(255, 0, 0);">&version</span>=1.1<span style="line-height: 1.8; color: rgb(255, 0, 0);">&q</span>=要翻译的文本</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">将上面的keyfrom 和key换成上面申请的内容,然后选择doctype,再输入要翻译的文本,就可以调用有道翻译API 进行翻译了。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">有道翻译提供了三种数据格式,这里我们只讲解两种,即xml 和json。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">5.1.1 xml 格式</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">关键代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);">public</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> youdaoDic(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyfrom</span> = "orchid"; <span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">申请APIKEY 时所填表的网站名称的内容</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$apikey</span> = "YourApiKey";<span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">从有道申请的APIKEY
//有道翻译-xml格式</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url_youdao</span> = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyfrom</span>.'&key='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$apikey</span>.'&type=data&doctype=xml&version=1.1&q='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$xmlStyle</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">simplexml_load_file</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url_youdao</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$errorCode</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$xmlStyle</span>-><span style="line-height: 1.8;">errorCode;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$paras</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$xmlStyle</span>->translation-><span style="line-height: 1.8;">paragraph;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$errorCode</span> == 0<span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$paras</span><span style="line-height: 1.8;">;
}</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">else</span><span style="line-height: 1.8;">{
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> "无法进行有效的翻译"<span style="line-height: 1.8;">;
}<br>}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>说明:</strong></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$xmlStyle = simplexml_load_file($url_youdao); // PHP 函数,将<span style="line-height: 1.8;">XML 文档载入对象中。</span></span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$errorCode = $xmlStyle->errorCode; // 获取错误码</span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$paras = $xmlStyle->translation->paragraph; // 获取翻译内容</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">5.1.2 json 格式</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">关键代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"> <span style="line-height: 1.8; color: rgb(0, 0, 255);">public</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> youdaoDic(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyfrom</span> = "orchid"; <span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">申请APIKEY时所填表的网站名称的内容</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$apikey</span> = "YourApiKey";<span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">从有道申请的APIKEY
//有道翻译-json格式</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url_youdao</span> = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyfrom</span>.'&key='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$apikey</span>.'&type=data&doctype=json&version=1.1&q='.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$jsonStyle</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">file_get_contents</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url_youdao</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span> = json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$jsonStyle</span>,<span style="line-height: 1.8; color: rgb(0, 0, 255);">true</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$errorCode</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>['errorCode'<span style="line-height: 1.8;">];
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = ''<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(<span style="line-height: 1.8; color: rgb(0, 0, 255);">isset</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$errorCode</span><span style="line-height: 1.8;">)){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">switch</span> (<span style="line-height: 1.8; color: rgb(128, 0, 128);">$errorCode</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">case</span> 0:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>['translation']['0'<span style="line-height: 1.8;">];
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">case</span> 20:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = '要翻译的文本过长'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">case</span> 30:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = '无法进行有效的翻译'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">case</span> 40:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = '不支持的语言类型'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">case</span> 50:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = '无效的key'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">default</span>:
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span> = '出现异常'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">break</span><span style="line-height: 1.8;">;
}
}
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$trans</span><span style="line-height: 1.8;">;
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>说明:</strong></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$jsonStyle = file_get_contents($url_youdao); // <span style="line-height: 1.8;">把整个文件读入一个字符串中</span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$result = json_decode($jsonStyle,true); // <span style="line-height: 1.8;">对JSON 格式的字符串进行编码</span></span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$errorCode = $result['errorCode']; // 获取错误码</span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">$trans = $result['translation']['0']; // 获取翻译结果</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.2 百度翻译API</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">百度翻译API提供UTF-8编码的PHP数组对应的标准JSON字符串,而且提供了 中->英,中->日,英->中,日->中 四种互译,比有道翻译多了一种。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">关键代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"> <span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">百度翻译</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">public</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> baiduDic(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$from</span>="auto",<span style="line-height: 1.8; color: rgb(128, 0, 128);">$to</span>="auto"<span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">首先对要翻译的文字进行 urlencode 处理</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word_code</span>=<span style="line-height: 1.8; color: rgb(0, 128, 128);">urlencode</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">注册的API Key</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$appid</span>="<span style="line-height: 1.8; color: rgb(255, 0, 0);">YourApiKey</span>"<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">生成翻译API的URL GET地址</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$baidu_url</span> = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$appid</span>."&q=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$word_code</span>."&from=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$from</span>."&to=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$to</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$text</span>=json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$this</span>->language_text(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$baidu_url</span><span style="line-height: 1.8;">));
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$text</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$text</span>-><span style="line-height: 1.8;">trans_result;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$text</span>-><span style="line-height: 1.8;">dst;
}
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">百度翻译-获取目标URL所打印的内容</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">public</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> language_text(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(!<span style="line-height: 1.8; color: rgb(0, 128, 128);">function_exists</span>('file_get_contents'<span style="line-height: 1.8;">)){
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$file_contents</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">file_get_contents</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span><span style="line-height: 1.8;">);
}</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">else</span><span style="line-height: 1.8;">{
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">初始化一个cURL对象</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span> =<span style="line-height: 1.8;"> curl_init();
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$timeout</span> = 5<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">设置需要抓取的URL</span>
curl_setopt (<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_URL, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上</span>
curl_setopt (<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_RETURNTRANSFER, 1<span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">在发起连接前等待的时间,如果设置为0,则无限等待</span>
curl_setopt (<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_CONNECTTIMEOUT, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$timeout</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">运行cURL,请求网页</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$file_contents</span> = curl_exec(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">关闭URL请求</span>
curl_close(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">);
}
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$file_contents</span><span style="line-height: 1.8;">;
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>说明:</strong></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">这里包含了两个函数,baiduDic() 和 language_text()。</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>baiduDic() 函数:</strong></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$word_code=urlencode($word); // 首先对要翻译的文字进行 urlencode 处理</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$text=json_decode($this->language_text($baidu_url)); // 调用language_text() 函数获取目标URL所打印的内容,然后对JSON 格式的字符串进行编码</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">$text = $text->trans_result; //获取翻译结果数组</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">return $text->dst; //取第一个数组的dst 结果。</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>language_text() 函数:</strong></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">判断file_get_contents() 函数是否存在,如果存在,则使用该函数获取URL内容;如果不存在,则使用cURL 工具获取URL内容。具体参见代码。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">六、测试</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">有道翻译-xml 格式:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/27164342-fe08b5ac7a3a4eabb724b33b297aed50.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">有道翻译-json 格式:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/27164403-1df46e719de2457bbe392a713d0eb255.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">百度翻译:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/27164418-1dd7014d55b14a829b2dba2f3c555758.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">七、完整代码获取</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">请访问 <a href="http://pan.baidu.com/s/1c0s3Jby" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);"><span style="line-height: 1.8;"><strong>乐思乐享</strong></span></a> 官方网盘</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;"><strong>URL</strong>:</span><a href="http://pan.baidu.com/s/1c0s3Jby" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://pan.baidu.com/s/1c0s3Jby</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">八、关注</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请关注<span style="line-height: 1.8;"> </span><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong> </span>微信公众帐号,<span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong></span> 基于BAE 平台开发,针对于主流的微信功能进行开发测试。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">您可以关注 <span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong> </span>公众帐号进行功能测试,以及获取新的应用开发。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(0, 0, 0);">1. <span style="line-height: 25px; orphans: 2; widows: 2; font-stretch: normal;">登录微信客户端,通讯录 -> 添加朋友 -> 查找公众号 -></span><span style="line-height: 25px; orphans: 2; widows: 2; font-stretch: normal;"> <span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>zhuojinsz</strong></span></span><span style="line-height: 25px; orphans: 2; widows: 2; font-stretch: normal;">,查找并关注。</span></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">2. 扫描二维码:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201307/10222318-06e43efa48ec459bb7e05896726130af.jpg" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong></span><span style="line-height: 1.8;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"> </span>功能列表。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;"><img src="http://images.cnitblog.com/blog/370046/201306/06160124-051d8d406e254a5a956947ade6977f21.png" alt="" style="border: 0px; max-width: 900px;"></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"> </p><hr style="color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>David Camp</strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong><strong><strong>我们永远相信,分享是一种美德 | </strong></strong>We Believe, Great People Share Knowledge...</strong></span></p></div>
页:
[1]