阅读703 返回首页    go 人物


微信公众号三方平台开发代微信公众号接收消息事件并响应

于千万人之中遇见了你

愿与你留下恒久的记忆

关注袁威,见证奇迹

require_once(dirname(__FILE__). /wxBizMsgCrypt.php );

//encodingAesKey和token均为申请三方平台是所填写的内容

$appId = 三方平台appid ;

$timeStamp = empty ( $_GET [ timestamp ] ) ? "" : trim ($_GET [ timestamp ] );

$nonce = empty ( $_GET [ nonce ] ) ? "" : trim ( $_GET[ nonce ] );

$msg_sign = empty ( $_GET [ msg_signature ] ) ? "" : trim( $_GET [ msg_signature ] );

$pc = new \WXBizMsgCrypt ( $token, $encodingAesKey, $appId );

$postArr = $GLOBALS[ HTTP_RAW_POST_DATA ];

$msg = ;

$errCode= $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $postArr,$msg);

解密后,当$errCode返回值为0时,代表解密成功,下一步我们就需要对消息进行解析处理:

$postObj =simplexml_load_string($msg, SimpleXMLElement ,LIBXML_NOCDATA);

if (strtolower($postObj -> MsgType) == event ){

//如果是关注subscribe事件

if(strtolower($postObj->Event == subscribe )){

$public_name= strval($postObj->ToUserName);

$map[ public_name ]=$public_name;

$cont =M( Subscribe )->where($map)->find();

//回复用户消息

$content =$cont[ content ];

responseText($postObj,$content);

}

}

if(strtolower($postObj -> MsgType) == text &&trim($postObj->Content)== 图文 ){

//这一步可从数据库中查询得到

$arr = array(

array(

title => test1 ,

description => test1 ,

picUrl => https://mmbiz.qpic.cn/mmbiz/mLiaE7fSUysSbbqzicX2LVsLL1HsXMRV0m6uicfiaSX9Aic43BA5vnpFOBMWAoEuaVDicoOX4HzGT8OT5QK6DRs14VkQ/0 ,

url => https://mp.weixin.qq.com/s?__biz=MjM5NzY4MDc0MA==&tempkey=mKI6U0rlJZofvceyQdxTPAYtneMxKyhWy52ytbUZfOJzFEHMDqmYTQLQWrkrSRky&appmsgid=10000002&itemidx=1&sign=99baf31f45e2357af575c63b5b303b6a#wechat_redirect ,

),

array(

title => test2 ,

description => test2 ,

picUrl => https://mmbiz.qpic.cn/mmbiz_jpg/mLiaE7fSUysTFDEZQTOvXleYwYqFN1JeLwM66Zg7dHjK3aHQxdVtwGTJgzuKJRuZCBHljIvVLkvZ2CADJ6paJYQ/0?wx_fmt=jpeg ,

)

);

responseNews($postObj,$arr);

}

$public_name= strval($postObj->ToUserName);

$keyword = strval(trim($postObj-> Content));

$log[ public_name ]=$public_name;

$log[ keyword ] =array( like , % .$keyword. % );

$con =M( Keyword )->where($log)->select();

foreach ($con as $vo=> $k){

$conn=$con[$vo][ content ];

}

if($conn){

$content =$conn;

}else{

$lg[ public_name ]=$public_name;

$lg[ keyword ]= ;

$con =M( Keyword )->where($lg)->select();

foreach($con as $vo => $k){

$conn=$con[$vo][ content ];

}

$content =$conn;

}

responseText($postObj,$content);

以上用到的responseText()、responseNews()鉴于代码模块化,而且方便复用,所以单独用方法写出,其他不多说了,具体见代码:

1)回复文本消息

function responseText($postObj,$content){

$template ="

%s

$fromUser = $postObj ->ToUserName;

$toUser = $postObj -> FromUserName;

$time = time();

$msgType = text ;

$res =sprintf($template,$toUser,$fromUser,$time,$msgType,$content);

$appId = 三方平台appid ;

$pc = new \WXBizMsgCrypt ($token, $encodingAesKey, $appId );

$encryptMsg = ;

$errCode =$pc->encryptMsg($res,$_GET [ timestamp ], $_GET [ nonce ], $encryptMsg);

if($errCode ==0){

$res = $encryptMsg;

}

echo $res;

}

2)回复图文消息

function responseNews($postObj,$arr){

$toUser = $postObj -> FromUserName;

$fromUser = $postObj -> ToUserName;

$template ="

%s

".count($arr)."

";

foreach($arr as $k=>$v){

$template.="

}

$template.="

$time = time();

$msgType = news ;

$res =sprintf($template,$toUser,$fromUser,$time,$msgType);

$appId = 三方平台appid ;

$pc = new \WXBizMsgCrypt ($token, $encodingAesKey, $appId );

$encryptMsg = ;

$errCode =$pc->encryptMsg($res,$_GET [ timestamp ], $_GET [ nonce ], $encryptMsg);

if($errCode ==0){

$res = $encryptMsg;

}

echo $res;

}

需要注意的是,在代微信公众号实现其功能的时候,接收的消息都需要解密,对回复的内容也必须进行加密再进行回复。

接收消息事件完整代码:

public function reponseMsg(){

require_once(dirname(__FILE__). /wxBizMsgCrypt.php );

//encodingAesKey和token均为申请三方平台是所填写的内容

$appId = 三方平台appid ;

$timeStamp = empty ($_GET [ timestamp ] ) ? "" : trim ( $_GET [ timestamp ] );

$nonce = empty ( $_GET[ nonce ] ) ? "" : trim ( $_GET [ nonce ] );

$msg_sign = empty ($_GET [ msg_signature ] ) ? "" : trim ( $_GET [ msg_signature ] );

$pc = new\WXBizMsgCrypt ( $token, $encodingAesKey, $appId );

$postArr =$GLOBALS[ HTTP_RAW_POST_DATA ];

$msg = ;

$errCode =$pc->decryptMsg($msg_sign, $timeStamp, $nonce, $postArr,$msg);

if($errCode == 0){

//处理消息类型,并设置回复类型和内容

$postObj =simplexml_load_string($msg, SimpleXMLElement ,LIBXML_NOCDATA);

//判断该数据包是否是订阅(用户关注)的事件推送

if(strtolower($postObj -> MsgType) == event ){

//如果是关注subscribe事件

if(strtolower($postObj -> MsgType) == event ){

//如果是关注subscribe事件

if(strtolower($postObj->Event == subscribe )){

$public_name= strval($postObj->ToUserName);

$map[ public_name ]=$public_name;

$cont= M( Subscribe )->where($map)->find();

//回复用户消息

$content= $cont[ content ];

responseText($postObj,$content);

}

}

}

if(strtolower($postObj-> MsgType) == text && trim($postObj->Content)== 图文 ){

//这一步可从数据库中查询得到

$arr= array(

array(

title => test1 ,

description => test1 ,

picUrl => https://mmbiz.qpic.cn/mmbiz/mLiaE7fSUysSbbqzicX2LVsLL1HsXMRV0m6uicfiaSX9Aic43BA5vnpFOBMWAoEuaVDicoOX4HzGT8OT5QK6DRs14VkQ/0 ,

url => https://mp.weixin.qq.com/s?__biz=MjM5NzY4MDc0MA==&tempkey=mKI6U0rlJZofvceyQdxTPAYtneMxKyhWy52ytbUZfOJzFEHMDqmYTQLQWrkrSRky&appmsgid=10000002&itemidx=1&sign=99baf31f45e2357af575c63b5b303b6a#wechat_redirect ,

),

array(

title => test2 ,

description => test2 ,

picUrl => https://mmbiz.qpic.cn/mmbiz_jpg/mLiaE7fSUysTFDEZQTOvXleYwYqFN1JeLwM66Zg7dHjK3aHQxdVtwGTJgzuKJRuZCBHljIvVLkvZ2CADJ6paJYQ/0?wx_fmt=jpeg ,

)

);

responseNews($postObj,$arr);

}else{

$public_name=strval($postObj->ToUserName);

$keyword= strval(trim($postObj -> Content));

$log[ public_name ]=$public_name;

$log[ keyword ]= array( like , % .$keyword. % );

$con= M( Keyword )->where($log)->select();

foreach($con as $vo => $k){

$conn=$con[$vo][ content ];

}

if($conn){

$content= $conn;

}else{

$lg[ public_name ]=$public_name;

$lg[ keyword ]= ;

$con= M( Keyword )->where($lg)->select();

foreach($con as $vo => $k){

$conn=$con[$vo][ content ];

}

$content= $conn;

}

responseText($postObj,$content);

}

}

}

点击播放 GIF/150K

如果正合你意,如果你也喜欢,点赞or分享只在一瞬间~

最后更新:2017-10-08 01:52:19

  上一篇:go 微信公众号三方平台开发获取授权方的授权信息以及基本信息
  下一篇:go 微信公众号三方平台开发全网发布及全网发布接入检测