php 微信开发获取用户信息如何实现

2025-05-27 0 21

php 微信开发获取用户信息

获取用户信息的大致算法是

用户授权登录第三方网站,

重点:scope参数:
snsapi_basic 静默登录,不需要用户授权,只能获取到openid;
snsapi_userinfo ,需要用户点击授权,能获取到openid和所有用户信息

第一步:先获取用户的code值;
第二步:根据code值去获取access_token,每次请求的值都不一样,如果没有使用,每五分钟更新一次;
第三步:根据access_token获取用户信息

1.获取code代码实现:

php 微信开发获取用户信息如何实现

php 微信开发获取用户信息如何实现

getcode.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
if(isset($_session['user'])){

print_r($_session['user']);

exit;

}

$appid='wx1d7c6fcd6131143b3';

$redirect_url="http://www.antfortune.vip/callback.php";

$scope='snsapi_userinfo';//获取的方式;

$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($redirect_url).'&response_type=code&scope='.$scope.'&state=123#wechat_redirect';

header("location:".$url);

2、根据code获取access_token和openid

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
getopenid.php

<?php

//获取用户openid

$appid="your appid";

$appsecret="your appsecret";

$code=$_get['code'];

function getopenid($appid,$appsecret,$code){

$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".

$appsecret."&code=".$code."&grant_type=authorization_code";

$weixin=file_get_contents($url);//通过code换取网页授权access_token

$jsondecode=json_decode($weixin); //对json格式的字符串进行编码

$array = get_object_vars($jsondecode);//转换成数组

$openid = $array['openid'];//输出openid

return $openid;

}

echo getopenid($appid,$appsecret,$code);

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php 微信开发获取用户信息如何实现 https://www.kuaiidc.com/74003.html

相关文章

发表评论
暂无评论