php 企业微信指令回调借款_php 微信企业号回调模式开发

根据上篇http://blog.csdn.net/u014454539/article/details/44626123的介绍,我们对微信企业号基本配置,开发人员管理、应用管理等,有了基本的了解。在开发过程中,我们可以申请开发者测试号,进行开发,了解企业号的接口与作用。

今天我们接着介绍微信企业号回调模式的开发,这里的回调模式开发主要是介绍,用户在微信端发出请求,服务器进行响应。比如:微信菜单的点击事件、关键字的响应等。

开发语言:php

因为企业号刚出来的缘故,所以网上资料十分少,而企业号官方给的demo里面的Simple.php无法正常使用。

以下贴出我测试过的代码,并进行了面向对象的封装,可以给相关开发进行参考:

mytpl.php 功能:编辑处理文本、新闻模板

<?php

class Mytpl{

public function text_tpl($text_mes){

$str = "

".$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 功能:响应微信回调模式

include_once "./weiLib/WXBizMsgCrypt.php";

include_once "./myLib/mytpl.php";

class Weixin{

private $encodingAesKey = "xxxxxxxxxxxx";

private $token = "xxxxxxxxxxxx";

private $corpId = "xxxxxxxxxxxx";

private $sVerifyMsgSig;

private $sVerifyTimeStamp;

private $sVerifyNonce;

public function __construct(){

$this -> 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();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值