代码:
<?php
namespace weChatcontrollers; use Yii; use yiiaseController; use yiihelpersJson; class AuthController extends Controller { /** * @name 调起授权 */ public function actionGetAuth() { header("Access-Control-Allow-Origin: *"); $url = $this->getUrl(); header('location:' . $url); die; } /** * @name 获取URL * @return string */ public function getUrl() { $appid = "wxb75677f80ddf694c"; $redirect_uri = urlencode('https://wechat.ruishan666.com/'); $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; return $url; } /** * @name 授权成功后 ,获取用户信息,存数据库,存cookie * @throws Exception * @deprecated since version 2.0 2017-08-22 授权在ws中授权 */ public function actionSetAuth() { $key = md5('wechat_token');
#由前端获取code $code = Yii::$app->request->post('code');
$return = $this->getAccessToken($code); if (!isset($return['access_token'])) { echo Json::encode(array('status' => 0, 'msg' => $return['errmsg'])); exit; } else { Yii::$app->cache->set($key, $return, 7000); } $staff = $this->getMember($return['access_token'], $return['openid']);
#获取到用户信息,自行进行处理 } /** * @name 获取用户信息 * @param $access_token * @param $openid * @return string */ public function getMember($access_token, $openid) { $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"; $return = file_get_contents($url); return $return; } /** * @name 获取accesstoken * @param $code * @return mixed */ public function getAccessToken($code) { $appid = 'wxb75677f80ddf694c'; $secret = "00d15041f6uyt4014dd3036113789c86"; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code "; $return = file_get_contents($url); return Json::decode($return); } }
授权请求地址
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
redirect_uri:授权请求成功后回调的地址
appid:公众号的唯一标识
response_type:返回类型,填写code
scope:应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
state:重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect:无论直接打开还是做页面302重定向时候,必须带此参数
用户同意授权后
如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
code说明 :
code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。
用code获取access_token
请求方法
获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
appid:公众号的唯一标识
secret:appsecret
code:获取的code
grant_type=authorization_code
拉取用户信息(需scope为 snsapi_userinfo)
请求方法
http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
返回成功
{ "openid":" OPENID", " nickname": NICKNAME, "sex":"1", "province":"PROVINCE" "city":"CITY", "country":"COUNTRY", "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ], "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }