
二、用户授权获取code
必备资源:
$appid = ‘*****************";
$appsecret = ‘*************************";
注:redirect_url是授权后重定向的回调链接地址,请使用urlencode对链接进行处理。
在网站入口处加上配置的 $url = ‘https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=http%3A%2F%2Fjixian.c.zmit.cn%2F&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect";
并请求访问上诉 url 。
当用户授权后,回调设置的域名,并会在url参数中拼接我们所需要的code,我们直接用 $_GET[‘code"] 获取即可!
三、通过code获取网页授权access_token和openid
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$token = json_decode(file_get_contents($token_url));
$opendid= $token->openid;
$access_token = $token->access_token;
四、获取用户信息
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$opendid."⟨=zh_CN";
$info = json_decode(file_get_contents($info_url));
$data["name"] = $info->nickname;
$data["image"] = $info->headimgurl;
print_r($info);
五、当获取到用户的openid等信息后,我们就可以将其存入数据库,只要有openid了,就相当于微信用户已经登录该网站!
使用AppID和AppSecret获取的access_token,通过全局Access Token获取用户基本信息
1. 用户关注以及回复消息的时候,均可以获得用户的OpenID
<xml> <ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName> <FromUserName><![CDATA[ollB4jv7LA3tydjviJp5V9qTU_kA]]></FromUserName> <CreateTime>1372307736</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> <EventKey><![CDATA[]]></EventKey></xml>其中的FromUserName就是OpenID
{ "access_token": "NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qgfnmNtIZWSjSQ", "expires_in": 7200}3. 再使用全局ACCESS_TOKEN获取OpenID的详细信息 


以上就是本文针对php微信公众平台开发之获取用户基本信息的全部内容,希望大家喜欢。