微信开放平台-小程序权限结婚-全网发布-返回api消息错误解决方案

时间:2023-02-04 15:56:50

首先有2个地址:

授权事件接收URL  处理接收  ticket,授权【authorized】,取消授权【unauthorized】等一些检查   返回success即可

公众号消息与事件接收URL:处理  返回api消息  的验证

1,获取微信服务器推送过来的消息,并且解密

 1 function wxapp_decrypt_msg()
 2     {
 3         $encryptMsg =    file_get_contents('php://input');//微信推送过来的内容
 4 
 5         $xml_tree     =     new DOMDocument();
 6         $xml_tree->loadXML($encryptMsg);
 7 
 8 
 9         $ToUserName =     $xml_tree->getElementsByTagName('ToUserName');
10         $ToUserName =     $ToUserName->item(0)->nodeValue;
11 
12         $array_e     =     $xml_tree->getElementsByTagName('Encrypt');//解密内容
13         $encrypt     =     $array_e->item(0)->nodeValue;
14 
15         $timeStamp  =     empty($_GET['timestamp']) ? ""    : trim($_GET['timestamp']) ; 
16         $nonce      =     empty($_GET['nonce']) ? ""    : trim($_GET['nonce']) ; 
17         $msg_sign   =     empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;
18         $format     =     "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
19         $from_xml     =     sprintf($format, $encrypt);
20         $pc         =     new WXBizMsgCrypt(TOKEN, AESKEY, WXOPENAPPID);//开放平台的参数
21         $errCode     =     $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
22 
23         return array($errCode, $msg, $pc);//返回解密的内容
24     }

 

2,获取 QUERY_AUTH_CODE 并且给客服发送消息

 1 $errCode     =     wxapp_decrypt_msg();
 2         $msg         =    $errCode['1'];//解密的内容
 3         
 4         $xml_tree     =     new DOMDocument();
 5         $xml_tree->loadXML($msg);
 6 
 7         $Content     =    $xml_tree->getElementsByTagName('Content');
 8         $Content     =    $Content->item(0)->nodeValue;//微信发送的内容 包含 QUERY_AUTH_CODE:*****
 9 
10         $openid     =    $xml_tree->getElementsByTagName('FromUserName');
11         $openid     =    $openid->item(0)->nodeValue;//微信发送者的openid  给微信发送消息的时候 使用
12         if(stripos($Content, 'QUERY_AUTH_CODE:') !== false)
13         {
14             echo '';
15             $AuthCode     =    str_replace('QUERY_AUTH_CODE:', '', $Content);
16             $wxapp         =    wxapp_get_info($AuthCode, 1);//通过authcode 获取微信授权测试小程序授权给开放平台的token
17             $returnMsg     =    $AuthCode.'_from_api';//返回给微信的内容 $authCode.'_from_api' 后面_from_api 固定值
18             $token         =    $wxapp['authorization_info']['authorizer_access_token'];
19             $url         =    'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;
20             $pushMsg     =    '{"touser":"'.$openid.'","msgtype":"text","text":{"content":"'.$returnMsg.'"}}';
21             $pushJson     =    _public_http_curl_post($url, $pushMsg);
22             exit;
23         }