美文网首页
在Laravel 5.5下进行微信第三方开发(全网发布事件授权回

在Laravel 5.5下进行微信第三方开发(全网发布事件授权回

作者: 沧海99 | 来源:发表于2017-11-28 09:37 被阅读0次
    • 记录小程序全网发布事件消息回调接口的调通
            /*
             * 事件文本消息回调
             */
            public function eventNotify($appid, Request $request)
            {
                //
                Log::info('事件授权回调');
                $timeStamp = empty($request->timestamp) ? "" : trim($request->timestamp); // 时间戳
                $nonce = empty($request->nonce) ? "" : trim($request->nonce); // 随机字符
                $msg_sign = empty($request->msg_signature) ? "" : trim($request->msg_signature); // 签名(公钥)
                $encryptMsg = file_get_contents('php://input'); // xml
    
                $xml_tree = new \DOMDocument();
                $xml_tree->loadXML($encryptMsg);
                $array_e = $xml_tree->getElementsByTagName('Encrypt');
                $encrypt = $array_e->item(0)->nodeValue;
    
                $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
    
                $from_xml = sprintf($format, $encrypt);
    
                // 第三方收到公众号平台发送的消息
                $appId = config('wechat_appid.app_id');
                $appKey = config('wechat_appid.app_key');
                $appToken = config('wechat_appid.app_token');
    
                $msg = '';
                $pc = new Encrypt($appToken, $appKey, $appId);
                $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
                // 解密失败
                if ($errCode != 0) {
                    Log::info('-- ** -- 解密失败');
                    echo 'error';
    
                    return false;
                }
                echo 'success';
                $xml = new \DOMDocument();
                $xml->loadXML($msg);
                Log::debug($msg);
                $openid = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
                $Content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
                $query_auth_code = trim(str_replace("QUERY_AUTH_CODE:", "", $Content));
    
                if (!Cache::store('database')->has('component_access_token')) {
                    $this->component_access_token();
                }
                $accessToken = Cache::store('database')->get('component_access_token');
                $postData = array (
                    'component_appid' => $appId,
                    'authorization_code' => $query_auth_code,
                );
                $postData = json_encode($postData);
                $result = $this->curl_post($this->wx . '/cgi-bin/component/api_query_auth?component_access_token=' . $accessToken, $postData);
                $res = json_decode($result, true);
                $token = $res['authorization_info']['authorizer_access_token'];
                Log::notice($token);
                Log::warning($openid);
                $data = array (
                    'touser' => $openid,
                    'msgtype' => 'text',
                    'text' => ['content' => $query_auth_code . '_from_api']
                );
                Log::debug($appid);
                $data = json_encode($data);
                $t_result = $this->curl_post($this->wx . '/cgi-bin/message/custom/send?access_token=' . $token, $data);
            }
    

    相关文章

      网友评论

          本文标题:在Laravel 5.5下进行微信第三方开发(全网发布事件授权回

          本文链接:https://www.haomeiwen.com/subject/kflsbxtx.html