微信公众号网页获取用户信息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35713752/article/details/89854369

微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

效果图

html 代码:

<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
		<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
	</head>
	<body>
		<script type="text/javascript">
			$(document).ready(function() {
				var url = "http://weixin.njkeren.cn/open/login.php";
				$.post(url, {}, function(msg) {
					 window.location.href=msg;
				})
			});
		</script>
	</body>

</html>

login.php 代码:

<?php

	header("Content-Type:text/html;charset=utf8"); 
	header("Access-Control-Allow-Origin: *"); //解决跨域
	header('Access-Control-Allow-Methods:POST');// 响应类型  

    $appid = "wxec203d9218b4892a";
    $appsecret = "cfe989c3cf5d89767f257953b9e0eec0";
	
	$redirect_uri = urlencode ( 'http://weixin.njkeren.cn/open/getUserInfo.php');
	
	//第一步:取得code
	$codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
	
	function getJson($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
		return json_decode($output, true);
	}
	 print_r($codeUrl);
?>   

 

getUserInfo.php 代码:

<?php
	//header("Content-Type:text/html;charset=utf8"); 
	//header("Access-Control-Allow-Origin: *"); //解决跨域
	//header('Access-Control-Allow-Methods:POST');// 响应类型  
    
    $appid = "wxec203d9218b4892a";
    $appsecret = "cfe989c3cf5d89767f257953b9e0eec0";
	
	$code = $_GET["code"];
	//第一步:取得openid
	$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
	$oauth2 = getJson($oauth2Url);
	
	//第二步:根据全局access_token和openid查询用户信息  
	$access_token = $oauth2["access_token"];  
	$openid = $oauth2['openid'];  
	//print_r($oauth2);
	$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
	$userinfo = getJson($get_user_info_url);
	 
	//打印用户信息
	echo urldecode(json_encode($userinfo));
	 
	function getJson($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
		return json_decode($output, true);
	}
?>

正确时返回的JSON数据包如下:

{   
    "openid":" OPENID",
    " nickname": NICKNAME,
    "sex":"1",
    "province":"PROVINCE"
    "city":"CITY",
    "country":"COUNTRY",
    "headimgurl":       "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
    "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
    "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
参数 描述
openid 用户的唯一标识
nickname 用户昵称
sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
province 用户个人资料填写的省份
city 普通用户个人资料填写的城市
country 国家,如中国为CN
headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
展开阅读全文

没有更多推荐了,返回首页