require "indexModelclass.php";
$index = new indexAction;
class indexAction{
public function index()
{
$nonce = $_GET['nonce'];
$token = 'weixin';
$tiemstamp = $_GET['tiemstamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
$array = array($nonce,$tiemstamp,$token);
sort($array);
$str = sha1(implode($array));
if($str==$signature && $echostr){ //微信在第一次绑定URL的时候会发送4个参数来验证你的URL地址,验证通过后,之后只会发送三个参数,所以可以用来户触发用户事件
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
public function reponseMsg()
{
//1.获取到微信推送过来的数据,格式如下
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
/*
123456789
*/
$postObj = simplexml_load_string($postArr); //把微信推送过来的XML格式转化成对象格式数据;
if($postObj->MsgType =='event'){
/*
12345678
*/
// 如果是关注事件即刚关注微信公众号的时候
if($postObj->Event == 'subscribe'){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$create = time();
$msgType = 'text';
$content = '欢迎观众我的微信测试号';
$template = "
%s
";
$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
echo $info;
}
}
if($postObj->MsgType =='text' && $postObj->Content =='你是猪'){
/*
12345678
*/
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$create = time();
$msgType = 'text';
$content = '你才是猪,SB';
$template = "
%s
";
$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
echo $info;
}
if($postObj->MsgType=="text"){
switch($postObj->Content){
case 1:
$content = '战狼二最近很火1';
break;
case 2:
$content = '战狼二最近很火2';
break;
case 3:
$content = '战狼二最近很火3';
break;
case 4:
$content = '战狼二最近很火4';
break;
case $postObj->Content:
$content = $postObj->Content;
break;
}
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$create = time();
$msgType = 'text';
$template = "
%s
";
$info = sprintf($template,$toUser,$fromUser,$create,$msgType,$content);
echo $info;
}
}
}
$index ->index();
一键复制
编辑
Web IDE
原始数据
按行查看
历史