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

PHP 微信开发获取用户信息

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

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

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

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

1.获取code代码实现:

getcode.PHP

rush:PHP;"> if(isset($_SESSION['user'])){ print_r($_SESSION['user']); exit; }

$appid='wx1d7c6fcd6131143b3';

        $redirect_url="http://www.antfortune.vip/callback.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>";
        $s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e='snsapi_userinfo';//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的方式;


        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($redirect_url).'&response_type=code&s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e='.$s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e.'&state=123#wechat_redirect';

header("Location:".$url);

2、根据code获取access_token和openid

rush:PHP;"> 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,$code);

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...