微信公众号获取未关注的用户基本信息信息(已关注或者未关注都可以)

 
 
1、根据公众号的appid获取code
    $APPID=APPID;//公众号在微信的appid
    $REDIRECT_URI='http://www.ific.cc/check.php';//回调页面    
    // $scope='snsapi_base';
    $scope='snsapi_userinfo';//需要授权
    $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&redirect_uri=".urlencode($REDIRECT_URI)."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";
    header("Location:".$url);
特:
 -->此处必须修改公众号的获取用户信息的回调url

2.check.php页面
$code = $_GET['code'];
$state = $_GET['state'];

/*根据code获取用户openid*/
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx72e1ef917e46fc68&secret=eb209bfaa8effa31f4508cea9788f5d0&code=".$code."&grant_type=authorization_code";

$abs = file_get_contents($url);
$obj=json_decode($abs);
$access_token = $obj->access_token;
$openid = $obj->openid;
/*根据code获取用户openid end*/


/*根据用户openid获取用户基本信息*/
$abs_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$abs_url_data = file_get_contents($abs_url);
$obj_data=json_decode($abs_url_data);
echo $OpenId = $obj_data->openid;

echo $NickName = $obj_data->nickname;
/*根据用户openid获取用户基本信息*/ 

批注://这种方法适用于服务号或者其余的申请到接口的, 但是不适用于测试号。。。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值