微信网页授权
来源:互联网 发布:ubuntu虚拟机镜像下载 编辑:程序博客网 时间:2024/06/02 07:34
第一步新建code.php
注意此处有四个参数需要改
第一个:APPID是测试号appID
第二个REDIRECT_URL是urlencode转换后的服务器上user.php路径
第三个:SCOPE改为
snsapi_userinfo第四个:STATE改为123
$url="
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
第二步新建user.php
<?php
define('APPID',"wx1cabc9a3ae73ca08");
define('SECRET',"d30dcaa2e2358607a27c6911bfb5abe5");
//获取code
$code=$_GET['code'];
//echo $code;die;
//获取刷新refresh_token
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".SECRET."&code=".$code."&grant_type=authorization_code";
$file=file_get_contents($url);
//echo $file;
$file = json_decode($file,true);
//获取用户access_token和open_id
$url1="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=".APPID."&grant_type=refresh_token&refresh_token=".$file['refresh_token'];
$file1=file_get_contents($url1);
$file1 = json_decode($file1,true);
//print_r($file1);
//获取用户详细信息
$url2="https://api.weixin.qq.com/sns/userinfo?access_token=".$file1['access_token']."&openid=".$file1['openid']."&lang=zh_CN";
$file2=file_get_contents($url2);
$file2 = json_decode($file2,true);
print_r($file2);
?>
第三步 新建power.php
<?php
define("APPID", "wx1cabc9a3ae73ca08");
define("appsecret", "d30dcaa2e2358607a27c6911bfb5abe5");
//获取用户信息
$url="http://www.tjgbank.cn/weixin.bk/user.php";
//url转码
$url1=urlencode($url);
//生成获取用户详细信息连接
$str="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$url1."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
echo $str;
?>
第四步:新建wx.php
<?php
//获取token
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1cabc9a3ae73ca08&secret=d30dcaa2e2358607a27c6911bfb5abe5";
$str=file_get_contents($url);
//echo $str;die;
//$url1='{"access_token":"ryedz0NLrAUbiEGgEvQdEaGdrz4BVxsPz_5VxfLqDD5flngZZ0_MvkWqQi-YojL5CFDwBWpYAyNeCeobh5Oe-Zoj9pdF6XWHNRSmyPXgFfo-W8FGUDyFObC5-9obqG4rBBLdAAAWWG","expires_in":7200}';
$arr=json_decode($str,true);
//print_r($arr);
$token=$arr['access_token'];
//echo $token;die;
//生成自定义菜单
$url2='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token;
//$con=file_get_contents($url2);
//print_r($con);
$data=' {
"button":[
{
"type":"view",
"name":"项目",
"url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1cabc9a3ae73ca08&redirect_uri=http%3A%2F%2Fwww.tjgbank.cn%2Fweixin.bk%2Fuser.php&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
}]
}';
//curl模拟提交
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url2); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭CURL会话
var_dump($tmpInfo); // 返回数据
?>
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 微信网页授权
- 关于微信网页授权
- 微信开发---->网页授权
- 微信开发网页授权
- 微信网页授权登录
- 微信网页授权登录
- HDU 2874 Connections between cities LCA Tarjan 链式前向星
- PIC单片机入门_C语言编程技术
- libcurl多线程超时设置不安全
- 函数
- UML各种视图的简介
- 微信网页授权
- static_cast与dynamic_cast转换
- c++primer第十一章关联容器小结-11
- 从读手册开始让zynq板卡跑起linux(二)----利用initrd和initramfs分别制作根文件系统
- 分段函数
- 嵌套listview
- 利用flex实现骰子1-9点布局
- 在Web应用中创建Spring容器的两种方式
- 【Android - MD】之FloatingActionButton的使用