微信开放平台之公众号第三方平台探讨总结

1、申请地址:https://open.weixin.qq.com/

2、填写开发资料:

 

说明:授权事件接收URL,用于获取微信服务器每10分钟推送的ComponentVerifyTicket,该Tikict用于和appid,appscret获取三方平台的 component_access_token,而component_access_token 则用于获取预授权码pre_auth_code,授权码换取公众号的授权信,预授权码:该API用于使用授权码换取授权公众号的授权信息,并换取authorizer_access_tokenauthorizer_refresh_token。 授权码的获取,需要在用户在第三方平台授权页中完成授权流程后,在回调URI中通过URL参数提供给第三方平台方。授权成功会返回授权公众号的授权信息,值得注意的是,authorizer_refresh_token必须保存好,authorizer_refresh_token是被授权公众号获取authorizer_access_token(相当于普通的access_token)的凭证,否则需要重新授权才能获取。

3、配置

如下:(注:所有消息收发均要加解密,主动调用的接口不包括)

复制代码

public function sysmessage(){
import("@.ORG.aes.WXBizMsgCrypt");
        $wxData = $this->weixin_account;
        $encodingAesKey = $wxData['encodingAesKey'];
        $token = $wxData['token'];
        $appId = $wxData['appId'];
        $timeStamp  = empty($_GET['timestamp'])     ? ""    : trim($_GET['timestamp']) ; 
        $nonce      = empty($_GET['nonce'])     ? ""    : trim($_GET['nonce']) ; 
        $msg_sign   = empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;
        $encryptMsg = file_get_contents('php://input');
        $pc = new WXBizMsgCrypt($token, $encodingAesKey, $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) {
            //print("解密后: " . $msg . "\n");
            $xml = new DOMDocument();
            $xml->loadXML($msg);
            $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
            $component_verify_ticket = $array_e->item(0)->nodeValue;

            //logResult('解密后的component_verify_ticket是:'.$component_verify_ticket);

            $dateline = time();
            $date_time = date('Y-m-d H:i:s',time());
            $data = array(
                'component_verify_ticket'=>$component_verify_ticket,
                'token_expires'=>$dateline+600,
                'date_time'=>$date_time,
            );
            $res = M('weixin_account')->where(array('appId'=>$this->component_appid))->save($data);
            if($res)
            {
                $weixin_account = M('weixin_account')->where(array('appId'=>$this->component_appid))->find();
                S('weixin_account'.$this->component_appid,$weixin_account,600);
                echo 'success';
            }
            
        } else {
            //logResult('解密后失败:'.$errCode);
            //$res = M('weixin_account')->where(array('appId'=>$this->component_appid))->save(array('text'=>'fasle'));
            print($errCode . "\n");
        }

        die();
}

复制代码

获取授权跳转链接:

1

2

3

4

5

6

7

8

9

10

11

12

13

//获取授权回调URL

public function auth(){

    if (IS_POST) {

        $pre_auth_code $this->weObj->get_auth_code();      

        $token $this->weObj->getAccessToken();

        $callback = U('Index/oauth_back','',true,false,true);

        $url $this->weObj->getRedirect($callback,$pre_auth_code);

        $this->ajaxReturn($url);

    }else {

        $this->ajaxReturn('error');

    }

 

}

  

格式:

1

<a href="https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wx07068eax27ef6eed&pre_auth_code=preauthcode@@@lVMrqq7UdbhIQj1-IwlrtYSXIoIghSSgPrWQmSgzSVChVdQJgJ2iAInQOqURpqFU&redirect_uri=http%3A%2F%2Fsmg.xxx.cn%2Findex.php%3Fg%3DUser%26m%3DIndex%26a%3Doauth_back" id="authurl" style="display: inline;"><img src="https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon_button3_1.png"></a>

  

点击进行授权:

再点击登陆并且授权的时候,会要求用管理员微信扫一扫确认授权。

授权成功后就得到被授权公众号的授权信息了,包括appId,authorizer_access_token,authorizer_refresh_token,头像,公众号类型等信息,其中authorizer_refresh_token必须完整保存好。

这样就可以完成一个公众号的授权了。

3、处理消息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

    //公众号消息与事件接收URL

//此处为实例,逻辑需要自己处理,(注意加解密)

    public function eventmsg(){

            import("@.ORG.aes.WXBizMsgCrypt");

            $wxData $this->weixin_account;

            $encodingAesKey $wxData['encodingAesKey'];

            $token $wxData['token'];

            $appId $wxData['appId'];

            $timeStamp  empty($_GET['timestamp'])     ? ""    : trim($_GET['timestamp']) ;

            $nonce      empty($_GET['nonce'])     ? ""    : trim($_GET['nonce']) ;

            $msg_sign   empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;

             

            $encryptMsg file_get_contents('php://input');

            $pc new WXBizMsgCrypt($token$encodingAesKey$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) {

                    //print("解密后: " . $msg . "\n");

                    $xml new DOMDocument();

                    $xml->loadXML($msg);

                     

                    $array_e $xml->getElementsByTagName('Content');

                    $content $array_e->item(0)->nodeValue;

                 

                    $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;

     

                    //加密消息

                    $encryptMsg '';

                    $text = "<xml>

                    <ToUserName><![CDATA[$FromUserName]]></ToUserName>

                    <FromUserName><![CDATA[$ToUserName]]></FromUserName>

                    <CreateTime>$stime</CreateTime>

                    <MsgType><![CDATA[text]]></MsgType>

                    <Content><![CDATA[$contentx]]></Content>

                    </xml>";

                 

                    $errCode $pc->encryptMsg($text$timeStamp$nonce$encryptMsg);

                    echo $encryptMsg;

                        exit();

 

             

            else {

                print($errCode "\n");

                exit();

            }

    }   

  到此授权基本完成了。

 

4、全网发布检测

复制代码

         if ($MsgType=="text") {
                    $needle ='QUERY_AUTH_CODE:';
                    $tmparray = explode($needle,$content);
                    if(count($tmparray)>1){
                        //3、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串
                        //表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝                                
                        $contentx = str_replace ($needle,'',$content);
                        $info = $this->weObj->get_authorization_info($contentx);
                        $test_token = $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;
                        $this->https_post($url, $data);
                    
                    } else{
                        //2、模拟粉丝发送文本消息给专用测试公众号
                        $contentx = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
                    }
                }
                                    
                //1、模拟粉丝触发专用测试公众号的事件
                if ($MsgType=="event") {
                    $array_e4 = $xml->getElementsByTagName('Event');
                    $event = $array_e4->item(0)->nodeValue;
                    $contentx = $event."from_callback";
                    
                }

复制代码

 

效果:

 

检测

 

成功

到此全网发布完成

https://www.cnblogs.com/ansuns/p/4770092.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值