微信第三方登陸,無需注冊一鍵登錄,獲取用戶信息,PHP實現方法.


  今天講講利用微信oauth2實現第三方登陸的實現方法.

  先說說前提吧!

  首先你得是服務號,並且是經過認證的.這樣微信會給你很多第三方接口的權限,如果是訂閱號或者沒有認證的服務號那就不用想了!

  一開始你需要進入微信公眾平台開啟開發模式,並且填寫oauth2的回調地址,地址填寫你項目的域名就可以了.比如:www.baidu.com或zhidao.baidu.com.如果你的項目在二級域名就寫二級域名

  前端url授權地址,在url中填寫appid與你項目中方法中的oauth的地址,具體在下面的代碼中可以看到.

<ahref="https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://www.xxxxxx.com/action/function/oauth2&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">授權</a>

  再說后台邏輯,首先調用微信接口的SDK.(后面會有)

include('./Card/Common/class_weixin_adv.php');

  之后填入微信官方給的的appid與secret

$weixin=new class_weixin_adv("appid", "secret");

  初始化SDK的類,取到code,利用獲取到的code在獲取出openid 看下面代碼注釋!

$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code";
$res = $weixin->https_request($url);//調用SDK方法獲取到res 從中可以得到openid
$res=(json_decode($res, true));//轉換成array 方便調用openid

  繼續調用SDK方法,獲取到用戶信息.此時$row已經獲得用戶信息了 可以var_dump下看看鍵值方便存入數據庫

$row=$weixin->get_user_info($res['openid']);

  獲取用戶信息就大功告成了,但這還不夠.我們需要的是無需注冊!所以需要利用openid,openid屬於唯一憑證,每個用戶對不同的公眾號都有不同的openid.可以理解成用戶賬號的感覺.我這里用的是把openid存入cookie的解決方案,類似用戶登陸的感覺,一些關鍵數據驗證只需要與數據庫中的openid進行對比.其他的一些利用方法可以發揮大家的想象!可以跟我留言交流!

關於之前的a鏈接的授權,大家也可以判斷cookie是否存在openid,從而讓未授權用戶直接跳轉到該地址,省卻了用戶的一步操作.

下面是完整邏輯代碼,大家可以參考下!

     public function oauth2(){
           include('./Card/Common/class_weixin_adv.php');
            $weixin=new class_weixin_adv("appid", "secret");
            if (isset($_GET['code'])){
                $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code";
                $res = $weixin->https_request($url);
                $res=(json_decode($res, true));
                $row=$weixin->get_user_info($res['openid']);
                
                if ($row['openid']) {
                   //這里寫上邏輯,存入cookie,數據庫等操作
                    cookie('weixin',$row['openid'],25920000);
                   
                }else{
                    $this->error('授權出錯,請重新授權!');
                }
                


            }else{
                echo "NO CODE";
            }
            $this->display();
        }

SDK代碼:微信官方有手冊,我就不多講了,自己研究,很簡單的!.

<?php

/**
 * 微信SDK 
 * pan041ymail@gmail.com
 */

class class_weixin_adv
{
    var $appid = "";
    var $appsecret = "";

    //構造函數,獲取Access Token
    public function __construct($appid = NULL, $appsecret = NULL)
    {
        if($appid){
            $this->appid = $appid;
        }
        if($appsecret){
            $this->appsecret = $appsecret;
        }

        
        $this->lasttime = 1395049256;
        $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ";

        if (time() > ($this->lasttime + 7200)){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
            $res = $this->https_request($url);
            $result = json_decode($res, true);
            
            $this->access_token = $result["access_token"];
            $this->lasttime = time();
        }
    }
//獲取用戶基本信息
    public function get_user_info($openid)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->https_request($url);
        return json_decode($res, true);
    }

//https請求 public function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } }

 

轉載請注明出處:http://home.cnblogs.com/u/p-0day/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM