天天看點

微信授權登入流程

微信授權登入流程

1 公衆号注冊擷取appid  與secret

2 控制器操作 主要是個過程了解就行了

public  function  _initialize(){

//a.判斷session是否存在

if(!session("openid")){

//b. 判斷code是否存在

if(!isset($_GET(["code"])||empty($_GET["code"]))

//c.微信授權  靜默授權 或者是使用者點選按鈕通過授權

$getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=回調位址&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

//d.php跳轉授權

header('location:'.$getCodeUrl);

exit();

}

//e.通過code擷取openid等其他操作

$code = $_GET['code'];

$str ="https://api.weixin.qq.com/sns/oauth2/access_token".

            "?appid=appid".

            "&secret=secret".

            "&code=".$code.

            "&grant_type=authorization_code"

//f.拿到openid和其他資料

            $html = json_decode(file_get_contents($str),1);

            $_SESSION["openid"]=$html['openid'];

}

我這個沒有做token重新整理功能  有需要的時候自己根據經驗自己添加就行了  看着文檔做就ok了