慕粉0118
2017-03-05 17:41
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
// 获得参数signature nonce token timestamp
$nonce = $_GET['nonce'];
$token = 'family';
$timestamp = $_GET['timestamp'];
$signature = $_GET['signature'];
$echostr = $_GET['echostr'];
$arr = array($nonce, $token, $timestamp);
sort($arr);
$tempstr = implode('', $arr);
$tempstr = sha1($tempstr);
if($tempstr == $signature && $echostr)
{
// 第一次接入微信API接口时验证,因为第一次有四个参数发送过来,其他情况下发送三个
echo $echostr;
exit;
}else
{
//
$this->$responseMsg();
}
}
public function responseMsg()
{
// 获取微信推送过来的post数据(XML格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
// 处理消息类型,并设置回复类型和内容
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> 开发者微信
<FromUserName><![CDATA[FromUser]]></FromUserName> 发送发账号
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType> 消息类型
<Event><![CDATA[subscribe]]></Event> 事件类型
</xml>*/
// simplexml_load_string();将XML转换成对象
$postObj = simplexml_load_string($postArr);
// 判断该数据包是否是消息订阅的时间推送
if($postObj->MsgType == 'event')
{
// 判断是否是关注事件
if ($postObj->Event == 'subscribe') {
// 回复用户消息
$ToUserName = $postObj->FromUserName;
$FromUserName = $postObj->ToUserName;
$CreateTime = time();
$MsgType = 'text';
$Content = '欢迎光临小店';
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $ToUserName, $FromUserName, $CreateTime, $MsgType, $Content);
echo $info;
}
}
}
public function test()
{
$ToUserName = 'FromUserName';
$FromUserName = 'ToUserName';
$CreateTime = time();
$MsgType = 'text';
$Content = '欢迎光临小店';
$template = "<xml>
<ToUserName>%s</ToUserName>
<FromUserName>%s</FromUserName>
<CreateTime>%s</CreateTime>
<MsgType>%s</MsgType>
<Content>%s</Content>
</xml>";
$info = sprintf($template, $ToUserName, $FromUserName, $CreateTime, $MsgType, $Content);
echo $info;
}
}
请教,哪里出了问题,怎么关注后不显示回复消息呢
$this->$responseMsg();这里错了, 调用方法应该这样$this->responseMsg();其他的还没看
PHP实现微信公众平台开发—提升篇
64947 学习 · 371 问题
相似问题