博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信登陆代码
阅读量:7111 次
发布时间:2019-06-28

本文共 1680 字,大约阅读时间需要 5 分钟。

网页访问

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=http://dopan.net/ceshiweixin.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect
保存以下内容至文件,回调至这个网页
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
$code = $_GET['code'];
$state = $_GET['state'];
//换成自己的接口信息
$appid = 'xxx';
$appsecret = 'xxx';
if (empty($code)) $this->error('授权失败');
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
    echo '<h1>错误:</h1>'.$token->errcode;
    echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
    exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
    echo '<h1>错误:</h1>'.$access_token->errcode;
    echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
    exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info->errcode)) {
    echo '<h1>错误:</h1>'.$user_info->errcode;
    echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
    exit;
}
//打印用户信息
echo '<pre>';
print_r($user_info);
echo '</pre>';
?>
</body>
</html>

如果微信开发的时候碰到了redirect_uri 参数错误的问题,尝试一下方法

如果授权文件目录为www.xxxx.com/xxx/xxx/OAuth2.0.PHP,那么回调域名就要

填www.xxxx.com/xxx/xxx

转载地址:http://akqhl.baihongyu.com/

你可能感兴趣的文章
线程安全与可重入函数的区别与联系
查看>>
vim使用记录
查看>>
php中 curl模拟post发送json并接收json
查看>>
简析J2EE应用程序数据库类设计模式
查看>>
十二周三次课 (3月14日)
查看>>
Hadoop子项目介绍
查看>>
【Interface&navigation】系统栏变暗(50)
查看>>
php判断用户输入验证码是否正确
查看>>
hbase hive整合
查看>>
搭建Linux操作系统+Oracle数据库的环境
查看>>
索引与排序
查看>>
第十二天 二维数组与多维数组
查看>>
什么是软件测试
查看>>
(摘录笔记)蓝桥杯算法训练
查看>>
linux PATH变量
查看>>
测试流程
查看>>
我的友情链接
查看>>
c++产生100以内的随机数
查看>>
java设计模式-可复用面向对象软件的基础(四)
查看>>
VS2010中安装Qt插件错误
查看>>