记微信开发(有道翻译api)
记微信开发(有道翻译api)
效果:
有道翻译api申请:
地址:http://fanyi.youdao.com/openapi
code:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "jiekou");//定义识别码
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();
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"];
//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->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$str_trans = mb_substr($keyword,0,2,"UTF-8");
$str_valid = mb_substr($keyword,0,-2,"UTF-8");
if($str_trans == "翻译" && !empty($str_valid)){
$word = mb_substr($keyword,2,202,"UTF-8");
//调用有道词典
$contentStr = $this->youdaoDic($word);
}elseif($str_trans != "翻译" && $keyword=="1"){
$contentStr = "公司简介";
}elseif($str_trans != "翻译" && $keyword=="2"){
$contentStr = "最新优惠";
}else{
$contentStr = "感谢您关注微信号:zhuzhu365";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}
public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "感谢您关注"."\n"."微信号:zhuzhu365"."\n"."回复:"."\n"."【1】公司简介"."\n"."【2】最新优惠"."\n"."更多内容,敬请期待...";
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}
public function responseText($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;
}
public function youdaoDic($word){
$keyfrom = "notfound"; //申请APIKEY 时所填表的网站名称的内容
$apikey = "1582872719"; //从有道申请的APIKEY
//有道翻译-xml格式
$url_youdao = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.$keyfrom.'&key='.$apikey.'&type=data&doctype=xml&version=1.1&q='.$word;
$xmlStyle = simplexml_load_file($url_youdao);
$errorCode = $xmlStyle->errorCode;
$paras = $xmlStyle->translation->paragraph;
if($errorCode == 0){
return $paras;
}else{
return "无法进行有效的翻译";
}
}
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;
}
}
}
?>