$nonce = $_GET['nonce'];$token = 'imooc';$timestamp = $_GET['timestamp'];$echostr = $_GET['echostr'];$signature = $_GET['signature'];//造成数组,而后按字典序排序
$array = array();$array = array($nonce, $timestamp, $token);sort($array);//拼接成字符串,sha1加密 ,而后与signature进行校验
$str = sha1( implode( $array) );if( $str == $signature && $echostr){//第一次接入weixin api接口的时候
echo $echostr;exit;
}else{//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];//2.处理消息类型,并设置回复类型和内容
/*
123456789
*/
$postObj = simplexml_load_string( $postArr);//$postObj->ToUserName = '';
//$postObj->FromUserName = '';
//$postObj->CreateTime = '';
//$postObj->MsgType = '';
//$postObj->Event = '';
// gh_e79a177814ed
//判断该数据包是不是订阅的事件推送
if( strtolower( $postObj->MsgType) == 'event'){//若是是关注 subscribe 事件
if( strtolower($postObj->Event == 'subscribe') ){//回复用户消息(纯文本格式)
$toUser = $postObj->FromUserName;$fromUser = $postObj->ToUserName;$time = time();$msgType = 'text';$content = '欢迎关注咱们的微信公众帐号'.$postObj->FromUserName.'-'.$postObj->ToUserName;$template = "
%s
";$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);echo $info;/*
12345678
*/}
}
}