1. 程式人生 > >微信第三方全網發布接入檢測(PHP)

微信第三方全網發布接入檢測(PHP)

rom 數據格式 觸發 his 小程序 all trac success mat

官方文檔 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318292&token=&lang=zh_CN

function getTicket(){
        $data = input(‘param.‘);
        trace($data,‘data‘);
        $msg_sign = input(‘msg_signature‘);
        $timeStamp = input(‘timestamp‘);
        $nonce = input(‘nonce‘);

        $encryptMsg = file_get_contents ( ‘php://input‘ );
        trace($encryptMsg,‘getTicket‘);

        //因為數據格式,先加密再解密


        $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appid);

        $xml_tree = new \DOMDocument();
        $xml_tree->loadXML($encryptMsg);
        $array_e = $xml_tree->getElementsByTagName(‘Encrypt‘);
        $encrypt = $array_e->item(0)->nodeValue;


        echo ‘success‘;

        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $encrypt);
        // 第三方收到公眾號平臺發送的消息
        $msg = ‘‘;
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
        trace($msg,"msg: " );
        if ($errCode == 0) {
            //print("解密後: " . $msg . "\n");
            $xml = new \DOMDocument();
            $xml->loadXML($msg);
            $array_e = $xml->getElementsByTagName(‘ComponentVerifyTicket‘);
            //保存下來
            $this->component_verify_ticket  = $array_e->item(0)->nodeValue;

            //獲取保存accessToken
            $this->component_access_token = $this->component->getComponentToken($this->component_verify_ticket);


        } else {
            print($errCode . "\n");
        }





    }

    //全網發布響應
    function callback(){
        $appid = input(‘appid‘);
        $msg_sign = input(‘msg_signature‘);
        $timeStamp = input(‘timestamp‘);
        $nonce = input(‘nonce‘);

        $encryptMsg = file_get_contents ( ‘php://input‘ );

        trace($encryptMsg,‘php://inpu‘);

        //解密
        $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appid);
        $msg = ‘‘;
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg);

        trace($msg,"3解密後: " );
        $response = json_decode(json_encode(simplexml_load_string($msg, ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true);

        //生成返回公眾號的消息
        $res_msg = $textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[text]]></MsgType> 
            <Content><![CDATA[%s]]></Content>
            </xml>";

        //判斷事件

        //2模擬粉絲發送文本消息給專用測試公眾號

        if ($response[‘MsgType‘]=="text") {
            $needle =‘QUERY_AUTH_CODE:‘;
            $tmparray = explode($needle,$response[‘Content‘]);
            if(count($tmparray)>1){
                trace($response,"解密後: " );
                //3、模擬粉絲發送文本消息給專用測試公眾號,第三方平臺方需在5秒內返回空串
                //表明暫時不回復,然後再立即使用客服消息接口發送消息回復粉絲
                $contentx = str_replace ($needle,‘‘,$response[‘Content‘]);//將$query_auth_code$的值賦值給API所需的參數authorization_code
                $this->authorization_code = $contentx;//authorization_code
                trace($contentx,‘authorization_code‘);


                //使用授權碼換取公眾號或小程序的接口調用憑據和授權信息
                $postdata = array(
                    "component_appid"=>$this->appid,
                    "authorization_code"=>$this->authorization_code,
                );

                $this->component_access_token = $this->component->getAccessToken();

                trace($this->component_access_token,‘access_token‘);

                $component_return = send_post($this->authorizer_access_token_url.$this->component_access_token,$postdata);

                $component_return = json_decode($component_return,true);
                trace($component_return,‘$component_return‘);
                $this->authorizer_access_token = $test_token = $component_return[‘authorization_info‘][‘authorizer_access_token‘];
                $content_re = $contentx."_from_api";
                echo ‘‘;

                //調用客服接口

                $data = array(
                    "touser"=>$response[‘FromUserName‘],
                    "msgtype"=>"text",
                    "text" => array(
                                "content" =>$content_re
                    )
                );
                $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$test_token;
                $ret = send_post($url, $data);
                trace($ret,‘客服消息‘);

            } else{
                //2、模擬粉絲發送文本消息給專用測試公眾號
                $contentx = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";



                trace($response,"2模擬粉絲發送文本消息給專用測試公眾號: " );
                $responseText = sprintf( $textTpl, $response[ ‘FromUserName‘ ], $response[ ‘ToUserName‘ ], $response[ ‘CreateTime‘ ],  $contentx );
//                echo $responseText;
                $echo_msg=‘‘;
                $errCode = $pc->encryptMsg($responseText, $timeStamp, $nonce, $echo_msg);
                trace($responseText,"2222轉數組: " );
                echo $echo_msg;
            }
        }

        //1、模擬粉絲觸發專用測試公眾號的事件

        if ($response[‘MsgType‘] == ‘event‘){
            $content = $response[‘Event‘]."from_callback";

            trace($response,"111轉數組: " );
            $responseText = sprintf( $textTpl, $response[ ‘FromUserName‘ ], $response[ ‘ToUserName‘ ], $response[ ‘CreateTime‘ ],  $content );
            trace($responseText,"111: " );
//            echo $responseText;
            $errCode = $pc->encryptMsg($responseText, $timeStamp, $nonce, $echo_msg);

            echo $echo_msg;
        }


    }

  

微信第三方全網發布接入檢測(PHP)