Callback page Callback page

Home  >  Article  >  Backend Development  >  WeChat web page authorization interface code example

WeChat web page authorization interface code example

WBOY
WBOYOriginal
2016-07-28 08:25:55986browse
OAuth2.0网页授权演示 
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3b83200000000000&redirect_uri=http://www.domain.com/oauth2.php&resp/a>

Callback page

<?php
if (isset($_GET[&#39;code&#39;])){
  //  echo $_GET[&#39;code&#39;];
	
	//通过code换取token
	$code = $_GET[&#39;code&#39;];
	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3b0000000&secret=575e05512121xxxxxxxxxxx&code=$code&grant_type=authorization_code";
	$json = file_get_contents($url);
	$arr = json_decode($json,true);
	$token = $arr[&#39;access_token&#39;];
	$openid = $arr[&#39;openid&#39;];
	//拿到token后就可以获取用户基本信息了
	$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";
	$json = file_get_contents($url);//获取微信用户基本信息
	$arr = json_decode($json,true);
	$name = $arr[&#39;nickname&#39;];//昵称
	$imgURL = $arr[&#39;headimgurl&#39;];//头像地址
	$sex = $arr[&#39;sex&#39;];//性别
	$province = $arr[&#39;province&#39;];//用户个人资料填写的省份
	$city= $arr[&#39;city&#39;];//普通用户个人资料填写的城市
	$country= $arr[&#39;country&#39;];//国家,如中国为CN
	
	echo "OpenID:".$openid."<br/>";
	echo "昵称:".$name."<br/>"."头像地址:".$imgURL."<br/>"."性别:".$sex."<br/>"."省份:".$province."<br/>"."城市:".$city."<br/>";
	echo "<img src=&#39;".$imgURL."&#39; />";
	
}else{
    echo "NO CODE";
}
?>

The above introduces the WeChat web page authorization interface code example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn