当前位置:Gxlcms > PHP教程 > 微信接口配置token验证失败。

微信接口配置token验证失败。

时间:2021-07-01 10:21:17 帮助过:19人阅读

我在新浪sae申请了账号之后创建了一个应用作为测试微信接口使用,代码用的都是官方教程的,但是在提交服务器配置的时候不是连接失败就是token验证失败,地址和token我确认没有填写错误,实在找不出原因。

  1. <code>于是我在本地进行了一下测试,把新浪sae日志中心微信发送的get请求复制过来,加在本地的地址后面模仿微信的请求,!
  2. !
  3. ![图片描述][2]
  4. 结果发现,程序里接收的timestamp和nonce加上token的字符串按要求排序后和链接里的signature根本不相同。!
  5. !![图片描述][3]
  6. 有大神遇到过这个问题吗?还是我哪里操作不对?程序是照搬官方文档的啊。求指点啊!
  7. `</code>

/**

  • wechat php test
    */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{

  1. <code>public function valid()
  2. {
  3. $echoStr = $_GET["echostr"];
  4. //valid signature , option
  5. if($this->checkSignature()){
  6. echo $echoStr;
  7. exit;
  8. }
  9. }
  10. public function responseMsg()
  11. {
  12. //get post data, May be due to the different environments
  13. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  14. //extract post data
  15. if (!empty($postStr)){
  16. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  17. $fromUsername = $postObj->FromUserName;
  18. $toUsername = $postObj->ToUserName;
  19. $keyword = trim($postObj->Content);
  20. $time = time();
  21. $textTpl = "<xml>
  22. <tousername><!--[CDATA[%s]]--></tousername>
  23. <fromusername><!--[CDATA[%s]]--></fromusername>
  24. <createtime>%s</createtime>
  25. <msgtype><!--[CDATA[%s]]--></msgtype>
  26. <content><!--[CDATA[%s]]--></content>
  27. <funcflag>0</funcflag>
  28. </xml>";
  29. if(!empty( $keyword ))
  30. {
  31. $msgType = "text";
  32. $contentStr = "Welcome to wechat world!";
  33. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  34. echo $resultStr;
  35. }else{
  36. echo "Input something...";
  37. }
  38. }else {
  39. echo "";
  40. exit;
  41. }
  42. }
  43. private function checkSignature()
  44. {
  45. $signature = $_GET["signature"];
  46. $timestamp = $_GET["timestamp"];
  47. $nonce = $_GET["nonce"];
  48. $token = TOKEN;
  49. $tmpArr = array($token, $timestamp, $nonce);
  50. print_r($tmpArr);
  51. echo "<hr>";
  52. sort($tmpArr,SORT_STRING);
  53. print_r($tmpArr);
  54. echo "<hr>";
  55. $tmpStr = implode('', $tmpArr );
  56. echo $tmpStr;
  57. echo "<hr>";
  58. $tmpStr = sha1( $tmpStr );
  59. echo "sha1加密后:".$tmpStr;
  60. echo "<hr>";
  61. echo 'signature:'.$signature;
  62. if( $tmpStr == $signature ){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }</code>

}
`

回复内容:

我在新浪sae申请了账号之后创建了一个应用作为测试微信接口使用,代码用的都是官方教程的,但是在提交服务器配置的时候不是连接失败就是token验证失败,地址和token我确认没有填写错误,实在找不出原因。

  1. <code>于是我在本地进行了一下测试,把新浪sae日志中心微信发送的get请求复制过来,加在本地的地址后面模仿微信的请求,!
  2. !
  3. ![图片描述][2]
  4. 结果发现,程序里接收的timestamp和nonce加上token的字符串按要求排序后和链接里的signature根本不相同。!
  5. !![图片描述][3]
  6. 有大神遇到过这个问题吗?还是我哪里操作不对?程序是照搬官方文档的啊。求指点啊!
  7. `</code>

/**

  • wechat php test
    */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{

  1. <code>public function valid()
  2. {
  3. $echoStr = $_GET["echostr"];
  4. //valid signature , option
  5. if($this->checkSignature()){
  6. echo $echoStr;
  7. exit;
  8. }
  9. }
  10. public function responseMsg()
  11. {
  12. //get post data, May be due to the different environments
  13. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  14. //extract post data
  15. if (!empty($postStr)){
  16. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  17. $fromUsername = $postObj->FromUserName;
  18. $toUsername = $postObj->ToUserName;
  19. $keyword = trim($postObj->Content);
  20. $time = time();
  21. $textTpl = "<xml>
  22. <tousername><!--[CDATA[%s]]--></tousername>
  23. <fromusername><!--[CDATA[%s]]--></fromusername>
  24. <createtime>%s</createtime>
  25. <msgtype><!--[CDATA[%s]]--></msgtype>
  26. <content><!--[CDATA[%s]]--></content>
  27. <funcflag>0</funcflag>
  28. </xml>";
  29. if(!empty( $keyword ))
  30. {
  31. $msgType = "text";
  32. $contentStr = "Welcome to wechat world!";
  33. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  34. echo $resultStr;
  35. }else{
  36. echo "Input something...";
  37. }
  38. }else {
  39. echo "";
  40. exit;
  41. }
  42. }
  43. private function checkSignature()
  44. {
  45. $signature = $_GET["signature"];
  46. $timestamp = $_GET["timestamp"];
  47. $nonce = $_GET["nonce"];
  48. $token = TOKEN;
  49. $tmpArr = array($token, $timestamp, $nonce);
  50. print_r($tmpArr);
  51. echo "<hr>";
  52. sort($tmpArr,SORT_STRING);
  53. print_r($tmpArr);
  54. echo "<hr>";
  55. $tmpStr = implode('', $tmpArr );
  56. echo $tmpStr;
  57. echo "<hr>";
  58. $tmpStr = sha1( $tmpStr );
  59. echo "sha1加密后:".$tmpStr;
  60. echo "<hr>";
  61. echo 'signature:'.$signature;
  62. if( $tmpStr == $signature ){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }</code>

}
`

做微信这块最好不要用sae,sae好像要实名认证才能正常访问一些消息。

你直接

  1. <code><!--?php
  2. echo $_GET['echostr'];
  3. ?--></code>

sae需要实名认证,如果不进行实名认证,SAE会在回复的内容中会带上干扰的html内容信息,从而导致Token验证失败或者该公众号暂时无法提供服务,请稍后再试。

ase需要实名认证后,才能验证成功的

人气教程排行