3,143
社区成员
发帖
与我相关
我的任务
分享
public function getBaseInfo()
{
\Think\Log::record("PublicController:getBaseInfo called", "DEBUG");
//1、获取code
$appid = C('APPID');
$redirect_url = urlencode(C('SERVER_ADDR').'/index.php/Weixin/Public/getUserOpenId');
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
header('location:'.$url);
exit();
}
//获取用户openid
public function getUserOpenId()
{
\Think\Log::record("PublicController:getUserOpenId called", "DEBUG");
$appid = C('APPID');
$appsecret = C('APPSECRET');
$code = $_GET['code']; //从上面函数getBaseInfo获取得到
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code ";
//3、获取openid
$res = json_decode(httpGet($url));
$unionId = WxAccessTokenController::getInstance()->getGZHUnionid($res->openid);
$this->assign('unionId', $unionId);
$this->display('login');
}
以上是我的代码,你应该参考静默授权,先获取code,通过code在获取openid