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

}

?>

  

posted on 2019-06-26 10:00  陌上冬雪  阅读(5040)  评论(0编辑  收藏  举报

导航