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

2025-07-03 720

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

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

目前存放平台系统配置新服务号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.公众号基本配置(token验证失败)

微信服务器配置token验证php代码,微信服务器配置Token验证失败问题
<?php
define("TOKEN", "你自己的token");

$wechatObj = new CallbackAPI;
$wechatObj->valid();

class CallbackAPI {

    /**
     * 签名验证
     * @return [type] [description]
     */
     public function valid() {
        $echoStr = $_GET["echostr"];
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        //将token、timestamp、nonce按字典序排序
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        //对tmpStr进行sha1加密
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }
}

2.公众平台测试账号接口配置信息(token验证失败)

 

<?php 


 //微信服务器发送过来的 :接受get参数
 
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        //自己定义的TOKEN    
        $token = 'phplove1';

         $echostr   = $_GET['echostr'];
         @file_put_contents('Public/checksing.html', '<br/>'.$signature.'-'.$timestamp.'-'.$nonce.'-'.$token, FILE_APPEND);
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        @file_put_contents('Public/checksing.html', '<br/>'.$tmpStr.'--'.$echostr, FILE_APPEND);
       
           //进行比对
        if( $signature ){
            echo  $echostr;
        }else{
            return false;
        }
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"dtmb.taobao.com",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性

山喂资源分享 其它教程 微信服务器配置token验证php代码,微信服务器配置Token验证失败问题 https://www.023140.com/589.html

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务