微信测试号开发 服务器 token验证

测试号上填写url为配置的服务器验证的访问地址,token随便填 但要保证与自己服务器配置的token相同

编写本地token验证测试文件


/**
* token验证测试页面
* @return 默认信息
*/
public function index()
{

  //获得几个参数
  $token = '';//此处填写之前开发者配置的token
  $nonce = $_GET['nonce'];
  $timestamp = $_GET['timestamp'];
  $echostr = $_GET['echostr'];
  $signature = $_GET['signature'];

  //file_put_contents('weixin_log.txt', "IP=".$_SERVER['REMOTE_ADDR'].PHP_EOL,FILE_APPEND); //记录访问IP到log日志
  //file_put_contents('weixin_log.txt', "QUERY_STRING=".$_SERVER['QUERY_STRING'].PHP_EOL,FILE_APPEND);//记录请求字符串到log日志
  //file_put_contents('weixin_log.txt', '$_GET[echostr])='.htmlspecialchars($_GET['echostr']).PHP_EOL,FILE_APPEND); //记录是否获取到echostr参数


  //参数字典序排序
  $array = array();
  $array = array($nonce, $timestamp, $token);
  sort($array);
  //验证
  $str = sha1( implode( $array ) );//sha1加密
  //对比验证处理好的str与signature,若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。
  if( $str == $signature && $echostr ){
  ob_clean();
   echo $echostr;
 }
 else{
  //接入成功后的其他处理
  }

 }

原文地址:https://www.cnblogs.com/jiafeimao-dabai/p/7229891.html