微信第三方平台全网发布流程精华(一)

最近搞了第三方平台代开发小程序

平台基本信息

太基础的信息就先不说了,这里说一下要注意的地方:
在这里插入图片描述

1.授权事件接收URL 这个主要是用来接收官方每十分钟推送的ticket(用来后期获取token),另外后期需要接收客户授权小程序成功返回的appid
2.域名IP白名单要设置为开发服务器ip
3.在需要的就是消息加密token 解密key啊这些 具体加密解密的官方有sdk包(php的有bug 我改过来了 有需要的可以评论找我 )

话不多说 直接上代码!!!

	//消息加解密密钥
    static private $encodingAesKey = "xxxxxxxxxxxxxxxxxxxxxxxx";

    //消息验证令牌
    static private $token = "xxxxxxxxxxxxxxxxxxxxxxxx";

    // 第三方平台 appid
    static private $appId = "xxxxxxxxxxxxxxxxxxxxxxxx";

    // 第三方平台 appsecret
    static private $appsecret = "xxxxxxxxxxxxxxxxxxxxxxxx";

    /**
     * 授权事件接收URL (获取ticket与接收appid)
     * @access public
     *
     */
    public function verify_ticket(Request $request){
        $data = $request->all();

        $encryptMsg   = file_get_contents('php://input');
        
        $timeStamp    = array_key_exists("timestamp",$data)?$data['timestamp']:'';
        $nonce        = array_key_exists("nonce",$data)?$data['nonce']:'';
        $msg_sign     = array_key_exists("msg_signature",$data)?$data['msg_signature']:'';

        $pc = new \WXBizMsgCrypt(self::$token,self::$encodingAesKey,self::$appId);
        
         
        $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);
        file_put_contents(storage_path('logs/Mini/Mini.log'),'['.date('Y-m-d : h:i:s',time()).']'.$from_xml."\r\n",FILE_APPEND);
    
        // 第三方收到公众号平台发送的消息
        $msg = '';

        $errCode = $pc->decryptMsg($msg_sign,$timeStamp,$nonce,$from_xml,$msg);

        if ($errCode == 0) {
            
            $xml = new \DOMDocument();
            $xml->loadXML($msg);

            $array_a = $xml->getElementsByTagName("InfoType");
            $infoType = $array_a->item(0)->nodeValue;

            if($infoType == 'component_verify_ticket'){

                $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
                $component_verify_ticket = $array_e->item(0)->nodeValue;
                file_put_contents(storage_path('logs/Mini/ticket.log'), $component_verify_ticket);
                \Redisaa::set('component_verify_ticket',$component_verify_ticket);
                echo 'success';
            }elseif($infoType == 'notify_third_fasteregister'){
                $status = $xml->getElementsByTagName('status')->item(0)->nodeValue;
                if($status == 0){
                    $appid = $xml->getElementsByTagName('appid')->item(0)->nodeValue;
                    $auth_code = $xml->getElementsByTagName('auth_code')->item(0)->nodeValue;
                    $str = "APPID:$appid,AUTH_CODE:$auth_code";
                    file_put_contents(storage_path('logs/Mini/MiniAppid.log'), '['.date('Y-m-d : h:i:s',time()).']'.$str."\r\n",FILE_APPEND);
                    DB::table('shop_legal')->where('id',DB::table('shop_legal')->max('id'))->update(['status'=>'1','appid'=>$appid]);

                    //获取授权方的帐号基本信息
                    $res = $this->getAuthorizerInfo($appid);
                    if($res['status'] == 'error'){
                        file_put_contents(storage_path('logs/Mini/MiniInfo.log'),'['.date('Y-m-d : h:i:s',time()).']'.$res['info']."\r\n",FILE_APPEND);
                    }
                   
                }else{
                     file_put_contents(storage_path('logs/Mini/Minierror.log'), '['.date('Y-m-d : h:i:s',time()).']'.$status."\r\n",FILE_APPEND);
                     if(in_array($status,array_keys(self::$info))){
                        $msg = self::$info[$status];
                     }
                     DB::table('shop_legal')->where('id',DB::table('shop_legal')->max('id'))->update(['msg'=>$msg,'status'=>'2']);
                }
                echo 'success';
            }

            
         
        } else {
            file_put_contents(storage_path('logs/Mini/ticketerror.log'), $errCode);
            echo $errCode;
        }
        

    }


    /**
     * 公众号消息与事件接收URL
     * @access public
     *
     */
    public function callback()
    {
        
        $timeStamp  = empty($_GET['timestamp'])     ? ""    : trim($_GET['timestamp']) ;
        $nonce      = empty($_GET['nonce'])     ? ""    : trim($_GET['nonce']) ;
        $msg_sign   = empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;

        $appid   = empty($_GET['appid']) ? ""    : trim($_GET['appid']) ;
        file_put_contents(public_path().'/callback.txt','['.date('Y-m-d : H:i:s',time()).'appid]'.$appid."\r\n",FILE_APPEND);   
    

        $encryptMsg = file_get_contents('php://input');
        $pc = new \WXBizMsgCrypt(self::$token,self::$encodingAesKey,self::$appId);
        
        $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);
        
        // 第三方收到公众号平台发送的消息
        $msg = '';
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
        if ($errCode == 0) {
            $xml = new \DOMDocument();
            $xml->loadXML($msg);
            
            $array_e2 = $xml->getElementsByTagName('ToUserName');
            $ToUserName = $array_e2->item(0)->nodeValue;
            $array_e3 = $xml->getElementsByTagName('FromUserName');
            $FromUserName = $array_e3->item(0)->nodeValue;
            $array_e5 = $xml->getElementsByTagName('MsgType');
            $MsgType = $array_e5->item(0)->nodeValue;
            $nowTime = date('Y-m-d H:i:s');
            $contentx = '';
            file_put_contents(public_path().'/callback.txt','['.date('Y-m-d : H:i:s',time()).'msg]'.$msg."\r\n",FILE_APPEND);
            
            if($MsgType=="text") {
                $array_e = $xml->getElementsByTagName('Content');
                $content = $array_e->item(0)->nodeValue;
                $needle ='QUERY_AUTH_CODE:';
                $tmparray = explode($needle,$content);
                if(count($tmparray) > 1){
                    file_put_contents(public_path().'/callback.txt','['.date('Y-m-d : H:i:s',time()).'fensi]'.'粉丝'."\r\n",FILE_APPEND);
                    //3、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串
                    //表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
                    $contentx = str_replace($needle,'',$content);
                    $info = $this->getMiniAppInfo($contentx);

                    $test_token = $info['info']['authorizer_access_token'];
                    $content_re = $contentx."_from_api";
                    echo '';
                    $data = '{
                            "touser":"'.$FromUserName.'",
                            "msgtype":"text",
                            "text":
                            {
                                 "content":"'.$content_re.'"
                            }
                        }';
                    
                    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$test_token;
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_exec($ch);
                    curl_close($ch);
                }else{
                    //2、模拟粉丝发送文本消息给专用测试公众号
                    $contentx = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
                }
            }elseif($MsgType == "event"){ //1、模拟粉丝触发专用测试公众号的事件
                $array_e4 = $xml->getElementsByTagName('Event');
                $event = $array_e4->item(0)->nodeValue;
                $contentx = $event.'from_callback';
            }
            
            $text = "<xml>
            <ToUserName><![CDATA[$FromUserName]]></ToUserName>
            <FromUserName><![CDATA[$ToUserName]]></FromUserName>
            <CreateTime>".time()."</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[$contentx]]></Content>
                    </xml>";
            
            //加密消息
            $encryptMsg = '';
            $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
            echo $encryptMsg;
            
            exit();
        } else {
            file_put_contents(public_path().'/errCode.txt', $errCode,FILE_APPEND);
            exit();
        }
    }
     /**
    * 获取第三方平台access_token
    * @return string component_access_token
    */
    public static function getMiniToken(){
           $ticket = \Redisaa::get('component_verify_ticket');

           if(\Redisaa::exists('component_access_token') && time()<\Redisaa::get('expires_in')){

                $token = \Redisaa::get('component_access_token');
               
            }else{
        
                $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
          
                $data['component_appid']         = self::$appId;
                $data['component_appsecret']     = self::$appsecret;
                $data['component_verify_ticket'] = $ticket;

                $res = json_decode(postUrl($url,json_encode($data)),true);

                if(!array_key_exists('component_access_token',$res)) return false;

                $token = $res['component_access_token'];
                \Redisaa::set('component_access_token',$token);
                \Redisaa::set('expires_in',time()+$res['expires_in']);

            }

           return $token;
    }

上面的这三个 就是全网发布检测的代码,并附上获取token, 其中的WXBizMsgCrypt类就是官方的消息加密解密sdk(可以找我哦)


在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阿巳

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值