微信开发4——PHP实现PC扫码授权登陆获取用户信息
生活随笔
收集整理的這篇文章主要介紹了
微信开发4——PHP实现PC扫码授权登陆获取用户信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先須要申請,必須企業,個體工商戶,媒體等,微信開放平臺申請地址 https://open.weixin.qq.com/,要交300多的費用才能開通
1,編寫入口:微信開放平臺的掃碼登陸開放的接口可以自動生成PC掃碼頁面,你得到了官方通過審核的appid和appsecret后,先構造一個掃碼網址入口(這里以"一號店"的掃碼登陸頁面為例子)
https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect
訪問后
掃碼后手機端效果
?
入口代碼如下:
<?php //-------配置 $AppID = 'wxbdc5610cc59c1631'; $AppSecret = 'd4624c36333337afxxxxxxxxxxxxxx'; $callback = 'https://passport.yhd.com/wechat/callback.php'; //回調地址 //微信登錄 login.php session_start(); //-------生成唯一隨機串防CSRF攻擊 $state = md5(uniqid(rand(),TRUE)); $_SESSION["wx_state"] = $state; //存到SESSION $callback = urlencode($callback); $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect"; header("Location: $wxurl"); ?>回調文件callback.php
<?php //驗證CSRF攻擊 if($_GET['state']!=$_SESSION["wx_state"]){exit("5001"); } $AppID = 'wx33333333334d4'; $AppSecret = 'd4624c363333330547af5443d'; $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); $json = curl_exec($ch); curl_close($ch); $arr=json_decode($json,1); //得到 access_token 與 openid print_r($arr); $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); $json = curl_exec($ch); curl_close($ch); $arr=json_decode($json,1); 得到 用戶資料 print_r($arr); ?>PC端最終效果如下:
?
如有問題,請咨詢我的qq:1290851757,備注:csdn博客問問題的
?
總結
以上是生活随笔為你收集整理的微信开发4——PHP实现PC扫码授权登陆获取用户信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言如何把void换成汉字,有没有大佬
- 下一篇: Linux安装后初始优化步骤(仅适用于C