在微信公众平台基础设置里面设置服务器配置
服务器里面的代码
public function checkSignature(){
if(isset($_GET['echostr'])){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = '自己设置的令牌(token)';
$EncodingAESKey = '消息加密秘钥';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $_GET['echostr'];
}
}
$xml = file_get_contents('php://input', 'r');
$data = $this->toArray($xml);
// $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : file_get_contents("php://input");
Cache::set('checkSignature',$data);
$fromUsername = $data['FromUserName']; //请求消息的用户
$toUsername = $data['ToUserName']; //"我"的公众号id
if(isset($data['Content'])){
$keyword = trim($data['Content']); //消息内容
}
$time = time(); //时间戳
$msgtype = 'text'; //消息类型:文本
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
// Cache::set('checkSignature1','1');
if($data['MsgType']=='event'){
$contentStrq = "你好,欢迎关注E家陪诊! 请输入手机号 进行身份绑定";
$resultStrq = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
echo $resultStrq;
exit();
}else if($data['MsgType']=='text'){
// Cache::set('checkSignature1','2--'.$keyword);
// 这是绑定的手机号
if(is_mobile($keyword)){
$user=Db::name('user')->where(array('phone'=>$keyword))->find();
if($user){
// Cache::set('checkSignature1','3--');
Db::name('user')->where(array('phone'=>$keyword))->update(array('fromUsername'=>$fromUsername));
//查询是否是商家
$AdminModel=new AdminModel;
if($AdminModel->where(array('uid'=>$user['id']))->find()){
$contentStrq = "你好,手机号绑定成功 用户下单后 可接受订单消息";
$resultStrq = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
echo $resultStrq;
exit();
}
$contentStrq = "你好,手机号绑定成功 开通小程序消息 接单开关 可接受订单消息";
$resultStrq = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
echo $resultStrq;
exit();
}else{
Cache::set('checkSignature1','4');
$contentStrq = "你好,手机号绑定失败 可能无法获得消息提醒";
$resultStrq = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
echo $resultStrq;
exit();
}
}
}
// $this->sendmsg();
}