您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP微信开发之有道翻译实例代码

2024/4/5 2:42:07发布9次查看
首先,你需要去有道翻译api官网去申请key:http://fanyi.youdao.com/openapi?path=data-mode
得到key之后,就可以开始从该api获得查询的数据了(返回json还是xml,看个人喜好,这里我用的是json) 
下面我直接把responsemsg方法里,实现翻译的代码给出。
</pre><a target=_blank href="http://blog.csdn.net/misakaqunianxiatian/article/details/49401759" target="_blank"></a></p><p><pre name="code" class="php"> public function responsemsg(){      //get post data, may be due to the different environments   $poststr = $globals[http_raw_post_data]; //接收微信发来的xml数据     //extract post data  if(!empty($poststr)){          //解析post来的xml为一个对象$postobj    $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);        $fromusername = $postobj->fromusername; //请求消息的用户    $tousername = $postobj->tousername; //我(订阅号)的公众号id    $keyword = trim($postobj->content); //用户发给订阅号的消息内容    $time = time(); //时间戳    $msgtype = 'text'; //消息类型:文本    $texttpl = <xml>   <tousername><![cdata[%s]]></tousername>   <fromusername><![cdata[%s]]></fromusername>   <createtime>%s</createtime>   <msgtype><![cdata[%s]]></msgtype>   <content><![cdata[%s]]></content>   </xml>;      if($postobj->msgtype == 'event'){ //如果xml信息里消息类型为event     if($postobj->event == 'subscribe'){ //如果是订阅事件      $contentstr = 欢迎订阅misaka去年夏天!\n更多精彩内容:http://blog.csdn.net/misakaqunianxiatian;      $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);      echo $resultstr;      exit();     }    }      $which = mb_substr($keyword, 0, 2, 'utf-8');//获取要返回什么样的信息      if($which== 翻译){ //如果要进行翻译     $fanyi = $which;     $search = str_replace($fanyi, '', $keyword); //要翻译的关键字       $key = ; //在有道api申请的key     $keyfrom = ; //与key对应     $url = 'http://fanyi.youdao.com/openapi.do?keyfrom=' . $keyfrom . '&key=' . $key . '&type=data&doctype=json&version=1.1&q=' . urlencode($search);//调用有道翻译api           $json = file_get_contents($url);//也可以用curl来获取     $res = json_decode($json, true);       /**      * 以下从返回的数据中提取翻译结果      */     $contentstr = '【查询】' . $res['query'] . \n;     $contentstr .= 【翻译】\n . $res['translation'][0] . \n;       $str = '';     foreach($res['basic']['explains'] as $v){      $str .= $v . \n;     }     $contentstr .= 【基本释义】\n . $str;         if(isset($res['web'])){        foreach($res['web'] as $kk=>$vv){       sort($vv);       $res['web'][$kk] = $vv; //调节字段顺序      }        $str = '';      foreach($res['web'] as $v){       foreach($v as $k2=>$v2){        if($k2 == 0){         $str .= 【. $v2 .】\n;        }else{         foreach($v2 as $v3){          $str .= $v3 .\n;         }        }       }      }      $contentstr .= 【网络释义】\n . $str;          }       $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);     echo $resultstr;     exit();       }
注:关注的订阅号返回的信息里要换行,要使用“\n”。在你的网站空间里,将你的代码修改一下,就可以给订阅号发送比如“翻译汉堡包”,订阅号会调用有道的api来返回翻译结果,可以中英文互译,也可以是句子。
以上就是php微信开发之有道翻译实例代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product