4,251
社区成员
发帖
与我相关
我的任务
分享
public function getBaseInfo()
{
//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);
}
//获取用户openid
public function getUserOpenId()
{
$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));
$this->assign('openid', $res->openid);
$this->display('login');
}