1、接收消息 $gracewechat->getMsg();
使用 $gracewechat->getMsg();可以接收微信服务器发送给开发者服务器的消息并转换成对象形式的数据。数据格式如下:
{
"ToUserName":"gh_b663823c3664", //公众号id
"FromUserName":"op15jsy7D14d8BAvDTTLTdumuz_I", //用户id
"CreateTime":"1497338009", //消息时间
"MsgType":"event", //消息类型
"Event":"VIEW", //事件类型
"EventKey":"******", //事件值
"MenuId":"412966992" //消息 id
}$gracewechat 对象接收到消息后会利用 msg 属性保存消息内容(对象形式),同时也记录了一些常用属性:
$gracewechat->msg //全部消息数据 $gracewechat->openId //客户openId $gracewechat->ourOpenId //公众号id $gracewechat->msgType //消息类型 $gracewechat->msgContent //消息内容 $gracewechat->event //对应事件
演示代码
<?php
class wxController extends grace{
public function index(){
$gracewechat = tool('gracewechat');
$gracewechat->getMsg();
}
}2、使用 reTextMsg() 函数回复文本消息
参数 : 回复内容
3、使用 reItemMsg() 函数回复图文消息
参数 : 数组形式的图文消息,格式:
$msg = array( array( '标题...', '描述...', '图片地址', '链接地址' ), //更多条目.... );
完整代码演示
<?php
class wxController extends grace{
public function index(){
$gracewechat = tool('gracewechat');
$gracewechat->getMsg();
if($gracewechat->msgType == 'text'){
//根据文本内容回复文本消息
switch($gracewechat->msgContent){
case 'hi':
$gracewechat->reTextMsg('hi...');
break;
case '图文':
$msg = array(
array(
'MUI 视频教程 - app开发',
'power by hcoder.net',
'http://static.hcoder.net/public/course_images/57eb25950dea9.png',
'http://www.hcoder.net/course/info_211.html'
),
array(
'阿里云开放图标库使用教程',
'power by hcoder.net',
'http://static.hcoder.net/public/course_images/586c95c94b214.png',
'http://www.hcoder.net/tutorials/info_136.html'
)
);
$gracewechat->reItemMsg($msg);
break;
default :
$gracewechat->reTextMsg('您说的是 : '.$gracewechat->msgContent);
}
}
}
}