微信公众号开发:用户授权获取用户信息


获取用户授权用户信息中的assesstoken调用不限次数!!!有效期两小时

header("Content-type: text/html; charset=utf-8"); 
$openid='';
$username='';
$img='';
if($_COOKIE['<span style="font-family: Arial, Helvetica, sans-serif;">openid</span><span style="font-family: Arial, Helvetica, sans-serif;">']){</span>
$openid=$_COOKIE['<span style="font-family: Arial, Helvetica, sans-serif;">openid</span><span style="font-family: Arial, Helvetica, sans-serif;">'];</span>
$username=$_COOKIE['username'];
$img=$_COOKIE['img'];
}else{
$code = $_GET['code'];
$state = $_GET['state'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=公众号id&secret=公众号秘钥&code=' . $code . '&grant_type=authorization_code';
$result = null;
try {
    $result = curlGet($url);
    $obj = json_decode($result);
    $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $obj->access_token . "&openid=" . $obj->openid;
    //微信返回值
    $userObj = json_decode(curlGet($getInfoUrl));
} catch (Exception $e) {
    echo $e->getTraceAsString();

}
//echo $userObj->openid;die;
//echo $userObj->openid;die;
$openid=$userObj->openid;
$username=urlencode(str_replace(array("'", "\\"), array(''), $userObj->nickname));
$img=$userObj->headimgurl;
setcookie('uid',$userObj->openid,time()+3*24*60*60);
setcookie('username',$username,time()+3*24*60*60);
setcookie('img',$img,time()+3*24*60*60);
}

function curlGet($url, $method = 'get', $data = '')
{
    $ch = curl_init();
    $header = "Accept-Charset: utf-8";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $temp = curl_exec($ch);
    return $temp;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值