1.去花生壳注册账号免费领取一个域名,我的是161785yt21.51mypc.cn,买一个内网穿刺,将域名映射到服务器IP和端口.这是我的办法,有其他办法的可以忽略
2.先去这个网址申请一个测试公众号并关注,需将域名配置在公众号内两个地方,JS接口安全域名和网页授权获取用户基本信息,不然授权不通过
3.配置PHP环境,略过
4.写前端页面wx.html,开始调用授权页,标红的需修改为自己的测试号的appid和appsecret
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>登陆</title>
</head>
<script type="text/javascript">
window.onload = function(){
window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=此处填写测试号的appid
&redirect_uri=http%3A%2F%2F161785yt21.51mypc.cn%2Fwx.php
&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
};
</script>
</html>
5.写php后台调用授权页面wx.php
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
//第一步:用户同意授权,获取code
$code=$_REQUEST["code"];
echo "<h1>您的code是:<span style='text-decoration:none;color:red;'>".$code."</span><h1><br>";
//第二步:通过code换取网页授权access_token
$urlwx="https://api.weixin.qq.com/sns/oauth2/access_token?appid=此处填写测试公众号的appid" .
"&secret=此处填写测试公众号的appsecret&code=".$code."&grant_type=authorization_code";
$jsonwx=json_decode(file_get_contents($urlwx),true);
$access_token=$jsonwx["access_token"];
$expires_in=$jsonwx["expires_in"];
$refresh_token=$jsonwx["refresh_token"];
$openid=$jsonwx["openid"];
$scope=$jsonwx["scope"];
$unionid=$jsonwx["unionid"];
echo "<h1>您的access_token是:<span style='text-decoration:none;color:red;'>".$access_token."</span><h1><br>";
//第四步:拉取用户信息(需scope为 snsapi_userinfo)
$urlwx="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$userinfo=json_decode(file_get_contents($urlwx),true);
echo "<h1>您的openid是:<span style='text-decoration:none;color:red;'>".$userinfo["openid"]."</span><h1><br>";
echo "<h1>您的unionid是:<span style='text-decoration:none;color:red;'>".$userinfo["unionid"]."</span><h1><br>";
echo "<h1>您的姓名是:<span style='text-decoration:none;color:red;'>".$userinfo["nickname"]."</span><h1><br>";
echo "<h1>您所在城市是:<span style='text-decoration:none;color:red;'>".$userinfo["country"].$userinfo["province"].$userinfo["city"]."</span><h1><br>";
?>
6.微信点开授权页地址http://161785yt21.51mypc.cn/wx.html,返回如下界面成功调用微信授权
7.将地址http://161785yt21.51mypc.cn/wx.html发到测试号的菜单里
a.调用此接口获取access_token
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=此处输入测试号的appid
&secret=此处输入测试号的appsecret
b.进入此页生成菜单https://mp.weixin.qq.com/debug/,输入access_token,菜单定义json,返回为200,ok则生成菜单成功
{
"button": [
{
"type": "view",
"name": "授权",
"url": "http://161785yt21.51mypc.cn/wx.html"
}
]
}
8.进入公众号,点击授权可获取自己微信的信息
9.完事了~~~~~~