获取微信公众号Token

获取微信公众号Token   

  获取微信公众号Token

 

       define("TOKEN", "weixin");
        $wechatObj = new WXApiVerify();
        /*开始验证程序*/
        $wechatObj->valid();

<?php
/**
 * Class WXApiVerify
 */
class WXApiVerify
{
 
  /**
   * For weixin server validation 
   * @param bool $return 是否返回
   */
  public function valid($return=false)
    {
        $echoStr = isset($_GET["echostr"]) ? $_GET["echostr"]: '';
        if ($return) {
            if ($echoStr) {
              if ($this->checkSignature()) 
                return $echoStr;
              else
                return false;
            } else 
              return $this->checkSignature();
        } else {
            if ($echoStr) {
              if ($this->checkSignature())
                die($echoStr);
              else 
                die('no access');
            }  else {
              if ($this->checkSignature())
                return true;
              else
                die('no access');
            }
        }
        return false;
    }

  private function checkSignature()
  {
    $signature = isset($_GET["signature"])?$_GET["signature"]:'';
    $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
    $nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';     
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr,SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    if( $tmpStr == $signature ){
      return true;
    }else{
      return false;
    }
  }
  

}