【微信服务号开发】04.通过网页授权获取用户信息

配合这三篇实战操作
【微信服务号开发】01.接入指南
https://segmentfault.com/a/11...
【微信服务号开发】02.文字消息和图文消息自动回复实现
https://segmentfault.com/a/11...
【微信服务号开发】03.实战 回复知乎日报,返回8个最新知乎日报图文消息
https://segmentfault.com/a/11...

微信开发者工具

https://mp.weixin.qq.com/debu...

【微信服务号开发】04.通过网页授权获取用户信息_第1张图片

开发文档

https://mp.weixin.qq.com/wiki...

添加授权回调页面的域名


【微信服务号开发】04.通过网页授权获取用户信息_第2张图片

代码实现

把以下链接里面的参数替换掉

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 若提示“该链接无法访

【微信服务号开发】04.通过网页授权获取用户信息_第3张图片

替换以下参数:
appid
redirect_uri
scope

替换的代码

【微信服务号开发】04.通过网页授权获取用户信息_第4张图片

确认登录后

页面会访问这个url

http://starks.ngrok.wdevelop.cn/act.php?code=0615P8s314BY1P1CVSq31r98s315P8sI&state=STATE

通过code换取网页授权access_token

代码实现

require_once('Curl.php');
$code = $_GET['code'];
$appid = "****************************";
$secret = "****************************";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
echo "
";
$data = Curl::CurlGet($url);
$data = (array)json_decode($data);
var_dump($data);

返回数据

string(339) "{"access_token":"4_rBAOjGbsn_mnhXIP-5vu6dFk98tNG76faFqgvUSjwivpY8eSnvRlfXBSKilHA3vFNSXHA8kB9k0CWfHSYDid3g","expires_in":7200,"refresh_token":"4_PdBbZZl13PuA1w2sKIDAz4WhZcbozeYNcJfFDHBxfpsofo8b_1UfgS6H_cVqe1wQwfEEhmqTnQ_tzFScjTd3xg","openid":"oPs5ouLW3qg7P6CLj-jS7M1XVtSw","scope":"snsapi_userinfo","unionid":"o28P7ww-ZMphcik-5ZSbkCr_QTQw"}"

【微信服务号开发】04.通过网页授权获取用户信息_第5张图片

拉取用户信息(需scope为 snsapi_userinfo)

代码实现

$access_token = $data['access_token'];
$openid = $data['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$userdata = Curl::CurlGet($userurl);
$userdata = (array)json_decode($userdata);
var_dump($userdata);

返回数据

string(343) "{"openid":"oPs5ouLW3qg7P6CLj-jS7M1XVtSw","nickname":"stark.wang","sex":1,"language":"zh_CN","city":"","province":"北京","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTJFHGcYsxe4TjLibiaDEicB8l2vv869kNLvfQ4P8WbO6adBmdTAH1s4h0JaH1oEZhU5icBApC6pXclF4Q\/0","privilege":[],"unionid":"o28P7ww-ZMphcik-5ZSbkCr_QTQw"}"

转换成数组格式

array(10) {
  ["openid"]=>
  string(28) "****************************"
  ["nickname"]=>
  string(10) "stark.wang"
  ["sex"]=>
  int(1)
  ["language"]=>
  string(5) "zh_CN"
  ["city"]=>
  string(0) ""
  ["province"]=>
  string(6) "北京"
  ["country"]=>
  string(6) "中国"
  ["headimgurl"]=>
  string(124) "http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJFHGcYsxe4TjLibiaDEicB8l2vv869kNLvfQ4P8WbO6adBmdTAH1s4h0JaH1oEZhU5icBApC6pXclF4Q/0"
  ["privilege"]=>
  array(0) {
  }
  ["unionid"]=>
  string(28) "o28P7ww-ZMphcik-5ZSbkCr_QTQw"
}

全部代码

Curl.php
act.php
";
$data = Curl::CurlGet($url);
$data = (array)json_decode($data);
var_dump($data);


$access_token = $data['access_token'];
$openid = $data['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$userdata = Curl::CurlGet($userurl);
$userdata = (array)json_decode($userdata);
var_dump($userdata);

你可能感兴趣的:(微信开发,微信公众号开发)