利用消息获取机制即可识别用户关注及取关行为,您也可以利用获得的关注数据进行数据库记录、关注回复等功能的开发。
示例代码
<?php
class indexController extends grace{
public $graceWeChat;
public function __construct(){
parent::__construct();
$this->graceWeChat = new \phpGrace\tools\graceWeChat();
}
public function index(){
// 接收消息
$this->graceWeChat->deBug = true;
$this->graceWeChat->getMsg();
// 识别用户关注事件
// 判断消息类型为 event [事件] 类型
if($this->graceWeChat->msgType == 'event'){
switch($this->graceWeChat->event){
// 关注
case 'subscribe':
// 获取用户信息
$user = $this->graceWeChat->getUser($this->graceWeChat->openId);
// 初始化消息回复对象
$replyMessageObj = new \phpGrace\tools\graceWeChat\replyMessage( $this->graceWeChat);
// 关注回复示例
if(empty($user->errcode)){
$replyMessageObj->text($user->nickname.' 您好,感谢关注 ^_^');
}
// 您可以使用数据库记录用户信息
// 获取用户信息
// p($user);
// 此处可以利用接口调试工具观察用户数据 : https://mp.weixin.qq.com/debug/
// 调试 : Msgtype : event , Event : subscribe
// your code ...
// 取消关注
case 'unsubscribe':
// 取消关注代码.....
// 可以在数据库内改变用户关注的状态
break;
}
}
}
}
getUser() 获取用户信息函数
<?php
/**
* 功能 : 获取用户信息
* @param string $openId // 用户 openid
* @return object //对象形式的用户数据
* */