閱讀703 返回首頁    go 小米 go 小米6


微信公眾號三方平台開發代微信公眾號接收消息事件並響應

於千萬人之中遇見了你

願與你留下恒久的記憶

關注袁威,見證奇跡

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 微信公眾號三方平台開發全網發布及全網發布接入檢測