1、接收消息 $hcWeChat->getMsg();
使用 $hcWeChat->getMsg();可以接收微信服务器发送给开发者服务器的消息并转换成对象形式的数据。数据格式如下:
{
"ToUserName":"gh_b663823c3664",
"FromUserName":"op15jsy7D14d8BAvDTTLTdumuz_I",
"CreateTime":"1497338009",
"MsgType":"event",
"Event":"VIEW",
"EventKey":"http:\/\/wx.hcoder.net\/demo\/pay.php",
"MenuId":"412966992"
}
2、使用 reTextMsg() 函数回复文本消息
参数 : 回复内容
3、使用 reItemMsg() 函数回复图文消息
参数 : 数组形式的图文消息,格式:
$msg = array( array( '标题...', '描述...', '图片地址', '链接地址' ), //更多条目.... );
完整演示代码
<?php
include './hcWeChat/hcWeChat.php';
$hcWeChat = new hcWeChat();
/* 消息识别 */
$hcWeChat->getMsg();
//文本形式的消息处理
if($hcWeChat->msgType == 'text'){
//根据文本内容回复文本消息
switch($hcWeChat->msgContent){
case 'hi':
$hcWeChat->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'
)
);
$hcWeChat->reItemMsg($msg);
break;
default :
$hcWeChat->reTextMsg('您说的是 : '.$hcWeChat->msgContent);
}
}