微信公众号第三方开发平台(二)

微信公众号第三方开发平台(一)

之前的文章是获取调用接口的一些基本参数,继续开发微信开放平台。

一、授权公众号,获取公众号的授权参数

    public function tiaozhuan()
    {
        $info = getData('system_config', array('type' => 'pre_auth_code'), array(), 1);    获取保存在数据库中的pre_auth_code
        $pre_auth_code = $info['value'];
        $back_url = "http://www.xxxxxx.com/index.php/$APPID$/callback";   //后台设置的回调地址,用户扫完二维码之后会跳转到这个连接    
        $url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wxcda7bf3c4bf44cdc&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$back_url;
        header("Location:".$url);
    }

二、在回调地址中,获取公众号access,并获取公众号的信息

    //保存公众号信息
    private function saveOpenWeixin()
    {
        //获取公众号access
        $info = getData('system_config', array('type' => 'component_access_token'), array() , 1); //获取保存在数据库中的component_access_token
        $component_access_token = $info['value'];
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token;
        $auth_code = $_GET['auth_code'];
        $post = array(
            'component_appid' => $this->appid,    //应用的appid
            'authorization_code'    => $auth_code,
        );
        $ret=send_post($url, $post);
        $author = json_decode($ret, True);
        //获取公众号信息
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token;
        $authorAppid = $author['authorization_info']['authorizer_appid'];
        $post = array(
            'component_appid' => $this->appid,
            'authorizer_appid' => $authorAppid,
        );
        $ret = send_post($url, $post);
        $userInfo = json_decode($ret, True);
        
        //公众号信息
        $authorInfo = $author['authorization_info'];

    }

三、使用公众号的基本信息调用接口

//获取授权方的公众号帐号基本信息
    public function demo6()
    {
        $info = getData('system_config', array('type' => 'component_access_token'), array(), 1);  //获取保存在数据库中的component_access_token
        $token = $info['value'];
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$token;
        $post = array(
            'component_appid' => $this->appid,
            'authorizer_appid' => 'xxxxxxxxxx',  //公众号appid
        );
        $ret = send_post($url, $post);
        $res = json_decode($ret, True);
    }
    
    //微信账号网页授权
    public function demo8()
    {
        $appid = 'xxxx'; //公众号的appid
        $redirect = site_url('toupiao/'.$appid.'/callback');
        //$scope = 'snsapi_base';
        $scope = 'snsapi_userinfo';   //获取方式
        $state = 'omllz';
        $component_appid = $this->appid;  //应用的appid
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect.'&response_type=code&scope='.$scope.'&state='.$state.'&component_appid='.$component_appid.'#wechat_redirect';
        header("Location:".$url);
    }

这是我的个人网站今日Tech 喜欢科技新闻的朋友可以收藏下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值