<?php/** * Created by PhpStorm. * User: yihuaiyuan * Date: 16/9/9 * Time: 下午12:46 */ /** * 网页授权接口的详细步骤总结 */ /** * 准备工作 * * 1.有一台可以访问的服务器,最好能绑定域名,通过域名可以直接访问到 * 我这里是阿里云ecs * * 2.有一个微信公众平台的账号,最好是认证的服务号 * 我这里是微信开发者中心申请的测试账号 * * 3.准备好开发环境, * 我这里是php语言 */ /** * 网页授权接口介绍: * 基本类型和详细类型 * snsapi_base/snsapi_userinfo * 域名必须在微信公共平台上备案 * 网页授权-->修改授权回调网站域名:www.mydomain.com * snsapi_userinfo获取的内容更详细 * * 1)用户同意授权,获取code * 2)通过code获取网页授权access_token * 3)刷新access_token(如果需要) * 4)拉去用户信息(需要scope为snsapi_userinfo) */ /** * 从微信公众号拿到APPID和APPSECERT,然后在接口权限里面,把网页授权接口后面网页修改为我们自己的服务器地址 * 如果是认证的服务号,会有网页授权权限,可以不用去开发者中心申请测试号,直接在接口权限里面找到网页授权接口,点击修改 * 如果公众号未获得网页授权接口,可以再开发者中心申请测试账号,里面接口全开,而且网页地址可以是IP */ /** * * laravel场景 * * vim app/Http/routes.php(路由而已) * Route::get(‘/‘, ‘WeixinController@welcome‘); * * Route::get(‘/login‘, ‘WeixinController@login‘); * Route::get(‘/bind‘, ‘WeixinController@bind‘); * * Route::get(‘/getCode‘, ‘WeixinController@getCode‘); * * * vim app/Http/Controllers/WeixinController.php(具体代码) * class WeixinController extends Controller{ //attribute * 从微信公众平台获取appid和appsecertprivate $appid = " apppid ";private $appsecert = " appsecert "; //indexpublic function welcome(){ * 展示登录或者绑定的跳转refer页面return view(‘weixin.welcome‘);} //loginpublic function login(){ $appid = $this->appid; * 回调地址,须在公众平台网页授权域名下$redirect_uri = urlencode("http://weixin.domain.com/getCode"); $loginUrl = urlencode("https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=login#wechat_redirect"); //生成二维码$loginSrc = "http://b.bshare.cn/barCode?site=weixin&url=".$loginUrl; return view("weixin.Qrcode",[‘src‘=>$loginSrc]); }//bindpublic function bind(){$appid = $this->appid;$redirect_uri = urlencode("http://weixin.spaceyun.com/getCode"); $bindUrl = urlencode("https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=bind#wechat_redirect"); $bindSrc = "http://b.bshare.cn/barCode?site=weixin&url=".$bindUrl; return view("weixin.Qrcode",[‘src‘=>$bindSrc]);} //getCode,获取access_tokenpublic function getCode(){ * 根据场景state的值判断是绑定还是登录if($_GET[‘state‘] === ‘login‘){$this->loginWeixin($_GET[‘code‘]);}elseif($_GET[‘state‘] === ‘bind‘){$this->bindWeixin($_GET[‘code‘]);}else{dd($_GET[‘state‘]);}} //loginWeixinprivate function loginWeixin($code){$appid = $this->appid;$appsecert = $this->appsecert;$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code"; * curl模拟get请求$res = $this->http_curl($url);$return_data = json_decode($res, true);$access_token = $return_data[‘access_token‘];$openid = $return_data[‘openid‘]; //需要取数据库查询openid是否存在,如果不存在需要获取用户信息填到数据库,如果存在直接登录获取用户id,如果用户ID小于o,用微信昵称做用户名,如果存在用户ID查询用户信息 * 获取用户信息$getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $userInfo = $this->http_curl($getInfoUrl); echo "login<br/>";echo "<pre>";var_dump($userInfo);} //bindWeixinprivate function bindWeixin($code){ $appid = $this->appid;$appsecert = $this->appsecert; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url); $return_data = json_decode($res, true);$access_token = $return_data[‘access_token‘];$openid = $return_data[‘openid‘]; $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $userInfo = $this->http_curl($getInfoUrl); echo "bind<br/>";echo "<pre>";var_dump($userInfo);} private function http_curl($url){$curlobj = curl_init();curl_setopt($curlobj, CURLOPT_URL, $url);curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE); $output = curl_exec($curlobj);curl_close($curlobj); return $output;}} */
时间: 2024-10-25 04:47:59