<?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);
} } ?>

  

最新文章

  1. weblogic网页登录URL
  2. Objective-C ,C++,java中常用编码格式对比
  3. Hive on Spark运行环境搭建
  4. Ubuntu 改动bash
  5. css滑动门制作圆角按钮
  6. IoC容器Autofac正篇之简单实例
  7. service and intentservice
  8. Hibernate-----5、持久化对象
  9. ASP.NET Page执行顺序
  10. [原创]ssget过滤动态块的方式
  11. ubuntu16.04下安装windows10(傻瓜式)
  12. Gradle连接Maven仓库直接从仓库 更新JAR包
  13. windows下面配合thinkphp5.1.7的nginx.conf内容
  14. PHP实用代码片段(一)
  15. CentOS 配置集群机器之间SSH免密码登录
  16. 计蒜客 31434 - 广场车神 - [DP+前缀和]
  17. HDU 4320 Arcane Numbers 1(质因子包含)
  18. slenium使用鼠标+键盘事件或者双击实现代码
  19. eclipse Git配置
  20. iOS XMPPFramework 环境配置

热门文章

  1. 百度小程序-swiper组件
  2. python3运行报错:TypeError: Object of type &#39;type&#39; is not JSON serializable解决方法(详细)
  3. list的过滤操作
  4. python学习笔记(一)python简介和基础
  5. Oracle序列重置
  6. PHP curl_multi_getcontent函数
  7. 【HDU6662】Acesrc and Travel【树形DP】
  8. Pangu and Stones HihoCoder - 1636 区间DP
  9. python基础实现tcp文件传输
  10. DOM操作 、js获取id的内容 和修改原来的内容(innerHTML)