微信-網頁授權獲取用戶openid

第一步:用戶同意授權,獲取code


在確保微信公衆賬號擁有授權作用域(scope參數)的權限的前提下(服務號獲得高級接口後,默認帶有scope參數中的snsapi_base和snsapi_userinfo),引導關注者打開如下頁面:


https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

所以

先用調試接口生成自定義菜單,菜單json如下:

{

    "button":[

    {

         "name":"風信科技",

 "sub_button":[

          {

              "type":"click",

              "name":"關於風信",

              "key":"FS_V1_1001"

           },

           {

              "type":"click",

              "name":"我們的服務",

              "key":"FS_V1_1002"

           },

           {

              "type":"click",

              "name":"成功案例",

              "key":"FS_V1_1003"

           },

{

              "type":"click",

              "name":"解決方案",

              "key":"FS_V1_1004"

           },

{

              "type":"click",

              "name":"OA通訊錄",

              "key":"FS_V1_1005"

           }]

     },

     {

         "name":"風信產品",

 "sub_button":[

          {

              "type":"click",

              "name":"平臺產品",

              "key":"FS_V2_1001"

           },

           {

              "type":"click",

              "name":"領域產品",

              "key":"FS_V2_1002"

           },

           {

              "type":"click",

              "name":"行業產品",

              "key":"FS_V2_1003"

           },

{

              "type":"view",

              "name":"微辦公",

              "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID&redirect_uri=$URL&response_type=code&scope=snsapi_base&state=1#wechat_redirect"

           }]

     },

     {

         "name":"風信娛樂",

 "sub_button":[

          {

              "type":"click",

              "name":"抽獎活動",

              "key":"FS_V3_1001"

           },

           {

              "type":"click",

              "name":"查詢天氣",

              "key":"FS_V3_1002"

           },

           {

              "type":"view",

              "name":"更多活動",

              "url":"http://www.funsing.com/"

           }]

     }]

}

第二步,獲取openId,

public function index() {
        $code = I('get.code');
        $weid = $this->getOpenId($code);
        if ($weid == "") {
            $this->redirect('/Home/Index/login', array(), 0, "waiting...");
        } else {
            $result = json_decode($this->client->IsAccount(array("weId"=>$weid))->IsAccountResult);
            $info = $result->ErrMsg;
            $this->gotoPage($info, $weid);
        }
    }


//通過code取得openid
    public function getOpenId($code) {
        $urlpre = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appsecret."&grant_type=authorization_code&code=";
        $url = $urlpre.$code;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $jsoninfo = json_decode($output, true);
        //$access_token = $jsoninfo["access_token"];
        $openid = $jsoninfo["openid"];
        return $openid;       
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章