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 ...
随机推荐
- temp = yield i 这句话的意思?
def test(): i = 0 while i < 5: temp = yield i # print(temp) i+=1 t = test() print(t.__next__()) p ...
- npm 是干什么的?(非教程)
看了之后就很清楚什么叫NPM,以后它是干嘛的.谢谢楼主 网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么要使用 npm」.今天我就来讲讲这个话题. 本文目标读者是「不太 ...
- 重新调整动态vhdx占用的空间
优化vhd:https://docs.microsoft.com/en-us/powershell/module/hyper-v/optimize-vhd?view=win10-ps 1. 弹出vhd ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- php ucwords()函数 语法
php ucwords()函数 语法 作用:把每个单词的首字符转换为大写 语法:ucwords(string) 参数: 参数 描述 string 必须,规定要转换的字符串 说明:把字符串中每个单词的首 ...
- php strtoupper()函数 语法
php strtoupper()函数 语法 作用:把所有字符转换为大写 语法:strtoupper(string) 参数: 参数 描述 string 必须,规定要转换的字符串 说明:strtouppe ...
- php ltrim()函数 语法
php ltrim()函数 语法 ltrim()函数怎么用? php ltrim()函数用于删除字符串左边的空格或其他预定义字符,语法是ltrim(string,charlist),返回经过charl ...
- <三剑客> 老三:grep命令用法
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- [CSP-S模拟测试]:mine(DP)
题目描述 有一个$1$维的扫雷游戏,每个格子用$*$表示有雷,用$0/1/2$表示无雷并且相邻格子中有$0/1/2$个雷.给定一个仅包含$?$.$*$.$0$.$1$.$2$的字符串$s$,问有多少种 ...
- python打印9宫格,25宫格等奇数格,且横竖斜相加和相等
代码如下: #!/usr/bin/env python3#-*- coding:utf-8 -*-num = int(input('请输入一个奇数:'))# 定义一个长为num的列表high = [[ ...