PHP获取用户是否关注公众号。获取微信openid和用户信息
<?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和用户信息的更多相关文章
- (利用tempdata判断action是直接被访问还是重定向访问)防止微信活动中用户绕过关注公众号的环节
说明:这个不是在进行微信公众号开发,也就是说在不能获取用户openid的前提下做的下面操作 1.动机:最近有个微信活动(关注了服务号的可以免费领取礼品),要做这么一个功能,活动的入口在微信服务号的菜单 ...
- java后端判断用户是否关注公众号
/** * 判断用户是否关注了公众号 * @param openid * @return */ public static boolean judgeIsFollow(String openid){ ...
- 微信 公众号 小程序 授权 unionid 用户信息 实验总结
-*-*-*-*-*-*-*-*-*--*-*-*-1.小程序通过code获取用户openid的接口,如果用户曾经授权并未过期,或者用户关注过同主体的公众号,会带回unionID,但没有用户头像等信息 ...
- 微信H5页面内实现一键关注公众号
H5页面内实现关注公众号的微信JSSDK没有相关接口开放,因此就得动点脑筋来实现该功能了.下面的方法就是通过一种非常蹊跷的方式实现的. 首先,需要在公众号内发表一篇原创文章,注意是原创文章,然后由另一 ...
- php 微信登录 公众号 获取用户信息 微信网页授权
php 微信登录 公众号 获取用户信息 微信网页授权 先自己建立两个文件: index.php 和 getUserInfo.php index.php <?php //scope=snsap ...
- H5页面获取openid,完成支付公众号(未关注公众号)支付
一.页面授权 // 进入页面获取权限code function initAuthorizeCode() { var appid = $("#appid").val();//公众号a ...
- Python3 itchat微信获取好友、公众号、群聊的基础信息
Python3 itchat微信获取好友.公众号.群聊的基础信息 一.简介 安装 itchat pip install itchat 使用个人微信的过程当中主要有三种账号需要获取,分别为: 好友 公众 ...
- 微信公众号获取粉丝openid系统
做为一名开发人员,在测试当中也经常需要用到openid,但是微信公众号获取openid的方法也是特别麻烦!网页授权是最常见的方式, 但是网页授权的流程太复杂,不仅要开发,还要在公众号后台设置回调域名( ...
- 微信公众号获取openid(php实例)
微信公众号获取openid 公众号获取openid的方法跟小程序获取openid其实是一样的,只是code获取的方式不一样 小程序获取code: 用户授权登录时调用wx.login即可获取到code ...
随机推荐
- bootstrap响应式布局原理
百分比布局+媒体查询 首先通过媒体查询确认container的宽度,每个col-xx-xx都是通过百分比定义的,屏幕尺寸变化了,container就变化了,col自然就变了 Bootstrap的官方解 ...
- 阿里下一代云分析型数据库AnalyticDB入选Forrester云化数仓象限
前言 近期, 全球权威IT咨询机构Forrester发布"The Forrester Wave: CloudData Warehouse Q4 2018"研究报告,阿里巴巴分析型数 ...
- Oracle or Question Solve(二)
数据库常用语句和函数 ----update update()函数主要注意的是后面的where限制条件--例子:update tab_a a set a.v1 = (select b.v1 from t ...
- 小程序 js 判断 字符串 为空 null
判断字符串是否为空 1 2 3 4 5 var strings = ''; if (string.length == 0) { alert('不能为空'); } 判断字符串是否为“空”字符即用户输入了 ...
- 题解 SP4033 【PHONELST - Phone List】
水一发trie板子~ 先说这个题怎么套上板子 首先我们判断是否有前缀可以边插入边判断 当我们经过了一个完整的字符串(即当前节点到了一个有标记的节点上) 就是有前缀 我们当然也可以无脑先判断一发(比如我 ...
- nRF51822 之 Interrupt
nRF51822的中断使用在官方的例程中好像没有!
- Nginx (限速)限制并发、限制访问速率、限制流量
Nginx 限制并发访问速率流量,配置还是简单的,看下Nginx文档根据文中这三个模块对照看一下就可以,Nginx限速使用的是漏桶算法(感兴趣可以看下文末的参考资料),需要注意的是:当需要进行限速操作 ...
- 改MYSQL数据库时遇的错
//http://www.dongcoder.com/detail-1103326.html
- iview+vue 表格中添加图片
开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...
- 获取header中content-type的值
后台传过来的值需要根据content-Type的值来判定成功与否 获取header中content-Tyep的值 用res.header['Content-Type']