微信公衆號第三方開發平臺(二)

微信公衆號第三方開發平臺(一)php

以前的文章是獲取調用接口的一些基本參數,繼續開發微信開放平臺。數據庫

1、受權公衆號,獲取公衆號的受權參數

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);
    }

2、在回調地址中,獲取公衆號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'];

    }

3、使用公衆號的基本信息調用接口

//獲取受權方的公衆號賬號基本信息
    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 喜歡科技新聞的朋友能夠收藏下。json

相關文章
相關標籤/搜索