微信网页授权时通过OAuth2.0完成的,整个过程分为三步:
用户授权,获取code;
根据code获取access_token【可通过refresh_token刷新获取较长有效期】
通过access_token和openid获取用户信息
第一种简单封装:
<?php
/**
* 微信授权相关接口
*/
class Wechat {
//高级功能-》开发者模式-》获取
private $app_id = 'xxx';
private $app_secret = 'xxxxxxx';
/**
* 获取微信授权链接
*
* @param string $redirect_uri 跳转地址
* @param mixed $state 参数
*/
public function get_authorize_url($redirect_uri = '', $state = '')
{
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
}
/**
* 获取授权token
*
* @param string $code 通过get_authorize_url获取到的code
*/
pub