根据上篇http://blog.csdn.net/u014454539/article/details/44626123的介绍,我们对微信企业号基本配置,开发人员管理、应用管理等,有了基本的了解。在开发过程中,我们可以申请开发者测试号,进行开发,了解企业号的接口与作用。
今天我们接着介绍微信企业号回调模式的开发,这里的回调模式开发主要是介绍,用户在微信端发出请求,服务器进行响应。比如:微信菜单的点击事件、关键字的响应等。
开发语言:php
因为企业号刚出来的缘故,所以网上资料十分少,而企业号官方给的demo里面的Simple.php无法正常使用。
以下贴出我测试过的代码,并进行了面向对象的封装,可以给相关开发进行参考:
mytpl.php 功能:编辑处理文本、新闻模板
".$text_mes['CreateTime']."
";
return $str;
}
public function news_tpl($mes, $news_mes){
$allNews = "";
foreach($news_mes as $news){
$allNews.= "-
";
}
$str = "
".$mes['CreateTime']."
".count($news_mes)."
".$allNews."
";
return $str;
}
}
?>
weixin.php 功能:响应微信回调模式
sVerifyMsgSig = $_GET["msg_signature"];
$this -> sVerifyTimeStamp = $_GET["timestamp"];
$this -> sVerifyNonce = $_GET["nonce"];
}
public function valid(){
$wxcpt = new WXBizMsgCrypt($this -> token, $this -> encodingAesKey, $this -> corpId);
$sVerifyEchoStr = $_GET["echostr"];
$errCode = $wxcpt->VerifyURL($this -> sVerifyMsgSig, $this -> sVerifyTimeStamp, $this -> sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
// 验证URL成功,将sEchoStr返回
echo $sEchoStr;
} else {
print("ERR: " . $errCode . "\n\n");
}
}
public function responseMsg(){
$sReqData = file_get_contents("php://input");
$wxcpt = new WXBizMsgCrypt($this -> token, $this -> encodingAesKey, $this -> corpId);
//decrypt
$sMsg = ""; //解析之后的明文
$errCode = $wxcpt->DecryptMsg($this -> sVerifyMsgSig, $this -> sVerifyTimeStamp, $this -> sVerifyNonce, $sReqData, $sMsg);
if ($errCode == 0) {
if(!empty($sMsg)){
$postObj = simplexml_load_string($sMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
if(trim($postObj -> MsgType) == "text"){
$this -> responseText($postObj);
}else if(trim($postObj -> MsgType) == "event"){
$this -> responseEvent($postObj, $news_mes);
}
}
} else {
print($errCode . "\n\n");
}
}
public function responseText($postObj){
$wxcpt = new WXBizMsgCrypt($this -> token, $this -> encodingAesKey, $this -> corpId);
switch($postObj -> Content){
case "1001":
$mycontent="功能1";
break;
case "1002":
$mycontent="功能2";
$this -> returnText($postObj, $mycontent);
break;
default :
$mycontent="输入无效,请查看相应说明";
$this -> returnText($postObj, $mycontent);
break;
}
}
public function responseEvent($postObj){
if($postObj -> Event == "click"){
switch($postObj -> EventKey)
{
case get_interface_list:
$mycontent = "1001.功能1\n1002.功能2\n\n请输入对应编号查询!";
$this -> returnText($postObj, $mycontent);
break;
case message_board:
$message = "问题反馈";
$this -> returnText($postObj, $message);
break;
}
}
}
public function returnText($postObj, $content){
$wxcpt = new WXBizMsgCrypt($this -> token, $this -> encodingAesKey, $this -> corpId);
$mytpl = new Mytpl;
$text_mes = array("ToUserName" => $postObj->FromUserName, "FromUserName" => $this->corpId, "CreateTime" => $this->sVerifyTimeStamp, "Content" => $content);
$sRespData = $mytpl -> text_tpl($text_mes);
$sEncryptMsg = ""; //xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $this -> sVerifyTimeStamp, $this -> sVerifyNonce, $sEncryptMsg);
if ($errCode == 0) {
//file_put_contents('test.txt', $sEncryptMsg); //debug:查看smg
print($sEncryptMsg);
} else {
print($errCode . "\n\n");
}
}
public function returnNews($postObj, $news_mes){
$wxcpt = new WXBizMsgCrypt($this -> token, $this -> encodingAesKey, $this -> corpId);
$mytpl = new Mytpl;
$mes = array("ToUserName" => $postObj->FromUserName, "FromUserName" => $this->corpId, "CreateTime" => $this->sVerifyTimeStamp);
$sRespData = $mytpl -> news_tpl($mes, $news_mes);
$sEncryptMsg = ""; //xml格式的密文
$errCode = $wxcpt->EncryptMsg($sRespData, $this -> sVerifyTimeStamp, $this -> sVerifyNonce, $sEncryptMsg);
if ($errCode == 0) {
//file_put_contents('test.txt', $sEncryptMsg); //debug:查看smg
print($sEncryptMsg);
} else {
print($errCode . "\n\n");
}
}
}
$weixin = new Weixin;
//$weixin -> valid();
$weixin -> responseMsg();
?>