<?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. Jenkins之配置GitHub-Webhook2

    什么是持续集成(Continuous integration) 提出者Martin Fowler本人对持续集成是这样定义的:持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每 ...

  2. linux 下启动tomcat 时没有执行权限

    原因: 没有权限 解决 : chmod 777 *.sh Linux下启动tomcat

  3. Java JsonPath grab InvalidPathException in code, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathExceptio

    I am using JsonPath and am able to parse my data and get the values when the path provided is correc ...

  4. error C2065: ‘__in’ : undeclared identifier

    转自VC错误:http://www.vcerror.com/?p=1307 问题描述: 编译时出现: error C2065: '__in' : undeclared identifier error ...

  5. 三种数据库截取字段内容&&获取字符长度的函数如下

    if(databaseutil.getValue("database").equalsIgnoreCase("sqlserver")){ list =categ ...

  6. 项目搭建(三):自定义DLL

    说明:程序中有些自定义的控件类型在TestStack.White框架中没有涉及,需要引入自定义的DLL,通过鼠标点击事件处理 使用:将自定义的ClassLibrary2.dll拷贝到项目/bin/de ...

  7. xstart使用方法

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/YABIGNSHI/article/det ...

  8. SSH服务搭建、账号密码登录远程Linux虚拟机、基于密钥的安全验证(Windows_Xshell,Linux)

    问题1:如果是两台虚拟机ping不同且其中一个虚拟机是克隆的另一个,需要更改一下MAC地址,关机状态下 一> "编辑虚拟机设置" 一>" 网络适配器" ...

  9. Intellij IDEA 智能补全的 10 个姿势,简直不能太牛逼!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 一年多前,栈长那时候刚从 Eclipse 转型 IDEA 成功,前面转了好多次,都是失败史,都是泪.. 后面我就在微信公众号 ...

  10. pixi与lottie-web的benchmark测试

    生产版本 "dependencies": {     "lottie-web": "^5.5.7",     "pixi-spin ...