微信服务器配置token验证php代码,微信服务器配置Token验证失败问题

这篇博客主要讨论了在微信公众号平台配置新服务号时遇到的token验证失败问题。作者通过代码分析和日志检查,发现验证能够成功在测试服务器上执行,但在正式环境上遇到问题。博客中提到了可能的问题出在微信服务器的响应环节,并展示了相关源码和日志截图,以寻求解决方案。内容涉及微信OAuth2.0授权流程、session管理和服务器配置等。
摘要由CSDN通过智能技术生成

目前存放平台系统配置新服务号token验证一直不成功,写其他测试服务器路径可以成功.验证返回值也有,问题可能出在微信服务器响应者一块 求吊大的给个解释?tell me why?

下面是源码及日志截图

class WxapiAction extends Action {

public $wxcode ='';

public $wxconfig =array();

public $opensession =array();

public $dbprefix="";

function _initialize() {

ob_clean();

$this->dbprefix=C('DB_PREFIX');

//判断是否获取当前微信用户的基本信息

//session_destroy();

if(session("wxcode")){

$wxcode=session('wxcode');

$sessions=$wxcode."_openid";

$this->opensession= (array)session($sessions);

$this->wxconfig= $this->opensession['wxconfig'];

$this->wxcode= $this->wxconfig['num'];

$rows=M("wxconfig")->field("a.*,m.*")->alias("a")->join(C('DB_PREFIX')."info m on a.tenantcode=m.tenantcode")->where("num='".$this->wxcode."'")->find();

$theme=$this->wxconfig['theme'];

if($theme!=$rows['theme']){

$_SESSION[$sessions]['wxconfig']=$rows;

}

if(empty($this->opensession['openid'])||empty($this->wxconfig['theme']))

{

unset($_SESSION[$sessions]);

unset($_SESSION["wxconfig"]);

}

}

else{

//如果也不带参数,第一次访问授权

$code = $_GET['code'];

$wxcode = $_GET['wxc']; pre($code."|".$wxcode);//die;

//开启pc端(1)

//$wxcode= APP_DEBUG?'klclub168':$wxcode;

if(empty($wxcode)){

$this->logger("微信设置参数错误");

$this->redirect ("App/Services/Error",array("msg"=>urldecode("微信设置参数错误")));

exit;

}

$this->wxcode=$wxcode;

$this->wxconfig= $this->getwxconfig($wxcode);

//是否获取到微信帐号信息

if($code){

$this->getwxopenid();

$sessions=$wxcode."_openid";

$this->opensession= (array)session($sessions);

}

else{

//没有code,则拉起授权,请求授权

$appid=$this->wxconfig['appid'];

$rand_num=rand(20,'0123456789');

$redirect_uri=C("WEBURL").U('App/Wxredirect/code');

$url_now="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=snsapi_userinfo&state=".$wxcode."_".$rand_num."#wechat_redirect";pre($_SESSION);pre($url_now);//die;

header("Location: $url_now");

}

//开启pc端(2)

}

//echo $this->wxconfig['tenantcode'];

//pre($_SESSION);die;

// echo $this->wxcode.'
';

// echo $_SESSION['openid']['openid'];

// pre($_SESSION);

// pre($this->opensession);die;

$count=M('visit')->where("data='".date('Y-m-d 00:00:00')."'")->select();

if($count[0]['data']!==date('Y-m-d 00:00:00')){

$where['count']=1;

$where['data']=date('Y-m-d 00:00:00');

$where['tenantcode']=$this->wxconfig['tenantcode'];

$r=M('visit')->add($where);

}else{

$where['id']=$count[0]['id'];

$where['count']=$count[0]['count']+1;

$where['data']=date('Y-m-d 00:00:00');

$r=M('visit')->save($where);

}

$ceurl=C("WEBURL")."/".APP_NAME."/Tpl/";

$this->assign("tpl", $ceurl);

//地址目录资源模板路径

$sourceurl=C("WEBURL")."/".APP_NAME."/Tpl/App/".$this->wxconfig['theme'];

$this->assign("sourceurl", $sourceurl);

$this->assign("wxcode", $this->wxcode);

}

//菜单验证

public function verifyurl($url='')

{

if(empty($this->wxcode)|| empty($url))

{

$api =C('WEBURL').'/index.php/App/Services/Error';

header("Location:$api");

exit;

}

$ret=$this->wxconfig;

$api = $url;

if (!isset($_SESSION['openid']) ) {

$urlU = urlencode($url);

$api = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$ret['appid']."&redirect_uri=".$urlU."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";

}

header("Location:$api");

}

//验证签名

public function valid($ary){

$echoStr = isset($_GET["echostr"])?$_GET["echostr"]:$_POST['echostr'];

$signature = isset($_GET["signature"])?$_GET["signature"]:$_POST['signature']; //$_GET["signature"];

$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:$_POST['timestamp']; //$_GET["timestamp"];

$nonce = isset($_GET["nonce"])?$_GET["nonce"]:$_POST['nonce']; //$_GET["nonce"];

$token = $ary['token']; //是开发应用设置的token值

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr,SORT_STRING);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

$ret=array('str'=>$tmpStr,'c'=>$signature,'s'=>$tmpArr,'o'=>$echoStr);

$this->logger("验证".json_encode($ret));

if($tmpStr == $signature){

echo $echoStr;

exit();

}

}

class IndexAction extends WxapiAction {

function _initialize() {

parent::_initialize ();

}

public function index(){

$echoStr = isset($_GET["echostr"])?$_GET["echostr"]:$_POST['echostr'];

//$this->logger($echoStr."-token验证|".json_encode($this->wxconfig));

if (isset($_GET['echostr'])) {

$this->valid($this->wxconfig);

}else{

$this->responseMsg();

}

}

![图片描述][1]

bVEWww?w=1390&h=127

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值