<?php
/*
* 首先填写授权地址为当前网址
* 将$appid和$secret参数替换成自己公众号对应参数,需要外网可以访问服务器环境测试
*/
header("Content-Type:text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai"); //define('code', $_GET['code']);
$oauth = new oauth();
if (!empty($_GET['code'])) {
$res_json = $oauth->GetOpenid();
if ($res_json['subscribe'] != 1) {
$subscribe = "未关注";
} else {
$subscribe = "已关注";
} if ($res_json['sex'] == 1) {
$sex = "男";
} elseif ($res_json['sex'] == 2) {
$sex = "女";
} else {
$sex = "未知";
} echo '
<html>
<head>
<meta charset="utf-8">
<style>
.info{
width:60%;
height:80%;
}
.input{
background: #eaeaea none repeat scroll 0 0;
border: 1px solid #dedede;
border-radius: 12px;
box-shadow: none;
outline: medium none;
padding: 10px;
resize: none;
width: 100%;
}
</style>
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
</head>
<body>
<table border="0" class="info">
<tr>
<td>头像:</td>
<td><img src="' . $res_json['headimgurl'] . '" width=150 height=150></td>
</tr>
<tr>
<td>昵称:</td>
<td class="input">' . $res_json['nickname'] . '</td>
</tr>
<tr>
<td>性别:</td>
<td class="input">' . $sex . '</td>
</tr>
<tr>
<td>地址:</td>
<td class="input">' . $res_json['country'] . $res_json['province'] . $res_json['city'] . '</td>
</tr>
<tr>
<td>openid:</td>
<td class="input">' . $res_json['openid'] . '</td>
</tr>
<tr>
<td>关注:</td>
<td class="input">' . $subscribe . '</td>
</tr>
<tr>
<td>时间:</td>
<td class="input">' . date("Y-m-d H:i:s", $res_json['subscribe_time']) . '</td>
</tr>
<tr>
<td>备注:</td>
<td class="input">' . $res_json['remark'] . '</td>
</tr>
<tr>
<td>分组:</td>
<td class="input">' . $res_json['groupid'] . '</td>
</tr>
</table></body></html>';
} else {
$oauth->GET_Code();
} class oauth { public $appid = "xxxxxxxxx";
public $secret = "xxxxxxxxxxxxxx"; //获取用户openid
public function GetOpenid() {
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
$json_obj = $this->https_request($get_token_url);
// $access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];
//$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$res_json = $this->getAccessInfo($this->appid, $this->secret, $openid);
return $res_json;
} //发起获得code值链接
public function GET_Code() {
$get_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid;
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header("location:" . $get_url . "&redirect_uri=" . $url . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
} //开始获取用户基本信息包含是否关注
public function getAccessInfo($appid, $secret, $openid) {
//获取access_token参数
$json_token = $this->_getAccessToken($appid, $secret);
//获取用户基本信息包含是否关注
$get_user_info_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $json_token['access_token'] . '&openid=' . $openid . '&lang=zh_CN';
$json_info = $this->https_request($get_user_info_url);
return $json_info;
} //获取access_token
private function _getAccessToken($appid, $secret) {
$url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
$json = $this->https_request($url_get);
return $json;
} //通用数据处理方法
public function https_request($get_token_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
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_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return json_decode($temp, true);
} } ?>

  

PHP获取用户是否关注公众号。获取微信openid和用户信息的更多相关文章

  1. (利用tempdata判断action是直接被访问还是重定向访问)防止微信活动中用户绕过关注公众号的环节

    说明:这个不是在进行微信公众号开发,也就是说在不能获取用户openid的前提下做的下面操作 1.动机:最近有个微信活动(关注了服务号的可以免费领取礼品),要做这么一个功能,活动的入口在微信服务号的菜单 ...

  2. java后端判断用户是否关注公众号

    /** * 判断用户是否关注了公众号 * @param openid * @return */ public static boolean judgeIsFollow(String openid){ ...

  3. 微信 公众号 小程序 授权 unionid 用户信息 实验总结

    -*-*-*-*-*-*-*-*-*--*-*-*-1.小程序通过code获取用户openid的接口,如果用户曾经授权并未过期,或者用户关注过同主体的公众号,会带回unionID,但没有用户头像等信息 ...

  4. 微信H5页面内实现一键关注公众号

    H5页面内实现关注公众号的微信JSSDK没有相关接口开放,因此就得动点脑筋来实现该功能了.下面的方法就是通过一种非常蹊跷的方式实现的. 首先,需要在公众号内发表一篇原创文章,注意是原创文章,然后由另一 ...

  5. php 微信登录 公众号 获取用户信息 微信网页授权

    php 微信登录 公众号 获取用户信息 微信网页授权 先自己建立两个文件: index.php  和  getUserInfo.php index.php <?php //scope=snsap ...

  6. H5页面获取openid,完成支付公众号(未关注公众号)支付

    一.页面授权 // 进入页面获取权限code function initAuthorizeCode() { var appid = $("#appid").val();//公众号a ...

  7. Python3 itchat微信获取好友、公众号、群聊的基础信息

    Python3 itchat微信获取好友.公众号.群聊的基础信息 一.简介 安装 itchat pip install itchat 使用个人微信的过程当中主要有三种账号需要获取,分别为: 好友 公众 ...

  8. 微信公众号获取粉丝openid系统

    做为一名开发人员,在测试当中也经常需要用到openid,但是微信公众号获取openid的方法也是特别麻烦!网页授权是最常见的方式, 但是网页授权的流程太复杂,不仅要开发,还要在公众号后台设置回调域名( ...

  9. 微信公众号获取openid(php实例)

    微信公众号获取openid 公众号获取openid的方法跟小程序获取openid其实是一样的,只是code获取的方式不一样 小程序获取code: 用户授权登录时调用wx.login即可获取到code ...

随机推荐

  1. python 列表使用

    下面实现的类似于java中的数组: names[-2]表示实现倒数的第2个参数 names[-3,-1]表示实现-3到-1的值不包含-1 增删改查 下面代码实现列表的增删改查功能: 复制copy 深c ...

  2. boost location-dependent times

    1. local_date_time #include <boost/date_time/local_time/local_time.hpp> #include <iostream& ...

  3. 学习总结-Redis

    一,简介 redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统,它经常被拿来和Memcached相比较,但是Memcache ...

  4. ES6中类Class的super关键字

    super 关键字,既可以当作函数使用,也可以当作对象使用.在这两种情况下,它的用法完全不同. 1.super当做函数使用 super 作为函数调用时,代表父类的构造函数.ES6 要求,子类的构造函数 ...

  5. luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元

    求 $\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j)$   考虑欧拉反演: $\sum_{d|n}\varphi(d)=n$   $\Rightarrow \sum_{i ...

  6. react教程 — 组件的生命周期 和 执行顺序

    一.组件执行的生命周期:                  参考  https://www.cnblogs.com/soyxiaobi/p/9559117.html  或  https://www.c ...

  7. python中的encode()和decode()函数

    前言: 我们知道,计算机是以二进制为单位的,也就是说计算机只识别0和1,也就是我们平时在电脑上看到的文字,只有先变成0和1,计算机才会识别它的意思.这种数据和二进制的转换规则就是编码.计算机的发展中, ...

  8. Gym-100676E Time Limit Exceeded?

    原题链接 https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604 ********************* ...

  9. 左手Mongodb右手Redis 第一章,进入Mongodb和Redis的世界

    ---恢复内容开始--- 1,为什么要使用非关系型数据库,关系型数据库咋滴,不能用嘛? 存在即合理,非关系型数据库的出现,那说明关系型数据库不适用了. 非关系型数据库(NOSQL)-->Not ...

  10. APP运营怎么利用留存率等数据分析用户减少的原因?

    APP运营怎么利用留存率等数据分析用户减少的原因? 数据分析最核心的方法是作比较,因为绝对的数值在大多数场合下是没有意义的,通过在不同维度之间做数据的比较分析,能帮助开发者找到数据变化的原因.举一个典 ...