微信開放平臺-小程序權限結婚-全網發佈-返回api消息錯誤解決方案

首先有2個地址:php

受權事件接收URL  處理接收  ticket,受權【authorized】,取消受權【unauthorized】等一些檢查   返回success便可node

公衆號消息與事件接收URL:處理  返回api消息  的驗證小程序

1,獲取微信服務器推送過來的消息,而且解密api

 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         }
相關文章
相關標籤/搜索