微信开发 获取用户基本信息 php

来源:互联网 发布:淘宝小视频怎么上传 编辑:程序博客网 时间:2024/04/28 19:11

首先你的公众号必须是企业号

<?php if(isset($_SESSION['user'])){    print_r($_SESSION['user']);//判断是否已经登录exit;}$APPID='你的APPID';  $REDIRECT_URI='授权登陆后需要跳转的界面';//url中的特殊字符如&、/等需要转码$scope='snsapi_userinfo';//需要授权$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.$REDIRECT_URI.'&response_type=code&scope='.$scope.'&state=1'.$state.'#wechat_redirect';header("Location:".$url);?>


这个是跳转进来的php
<?php$appid = "<span style="font-family: Arial, Helvetica, sans-serif;">你的APPID</span><span style="font-family: Arial, Helvetica, sans-serif;">"; </span>$secret = "你的SECRET"; $code = $_GET["code"]; $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';//获取token的url$access_token = json_decode(file_get_contents($get_token_url));$access_tokens = $access_token->access_token;$openid = $access_token->openid;$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_tokens.'&openid='.$openid.'&lang=zh_CN'; //获取用户信息的url$user_info = json_decode(file_get_contents($get_user_info_url));$_SESSION['user'] = $user_info; echo $user_info->nickname;//得到用户信息echo $user_info->headimgurl;?>


0 0
原创粉丝点击