to ken 配置成功 就是关注不回复
关注不回复 不进else
关注不回复 不进else
2017-12-24
<?php
namespace app\index\controller;
class Index
{
public function index(){
//获得参数 signature nonce token timestamp echostr
$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{
$this->reponseMsg();
}
}
public function reponseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//½«xmlÊý¾Ý°üת³É¶ÔÏó
$postObj = simplexml_load_string($postStr);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
if(strtolower($postObj->MsgType) == 'event') {
//判断如果是关注
if(strtolower($postObj->Event) == 'subscribe'){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
$contentStr = "谢谢关注000";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}
}//reponseMsg end
}
举报