微信网页授权获取用户基本信息(snsapi_userinfo)
来源:互联网 发布:硬科幻 网络 编辑:程序博客网 时间:2024/06/14 16:34
snsapi_userinfo方式获取用户信息
config.php 存放appid和appsecret
<?php$config = [ 'appid' => '************', 'appsecret' => '*******************',];
test.php跳转并获取code
<?phprequire_once './config.php';$appid = $config['appid'];$redirect_uri = urlencode('http://www.haoyiya.cn/weixin/auth.php');//授权url$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";//如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。header("location:$url");exit;auth.php获取用户信息
<?phpheader("Content-type:text/html;charset=utf-8");require_once './config.php';//获取codeif (isset($_GET['code'])){ $code = $_GET['code'];}else{ die("NO CODE");}$obj = new snsapi_userinfo;//获取access_token和openid$res = $obj->access_token($config['appid'],$config['appsecret'],$code);//获取用户信息$userInfo = $obj->userInfo($res->access_token,$res->openid);//输出用户信息echo "<h1>";print_r($userInfo);echo "</h1>";/** * Class snsapi_userinfo * 获取用户信息 */class snsapi_userinfo{ //获取access_token和openid public function access_token($appid,$appsecret,$code){ $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $res = json_decode($this->httpGet($url)); return $res; } public function userInfo($access_token,$openid){ $userUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $res = json_decode($this->httpGet($userUrl)); return $res; } //curl方式获取返回值 public function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; }}
阅读全文
0 0
- 微信网页授权获取用户基本信息(snsapi_userinfo)
- 微信网页授权获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信网页授权获取用户基本信息
- Java微信网页授权授权获取用户基本信息
- 微信公众平台开发 网页授权获取用户基本信息
- 微信公众平台开发 网页授权获取用户基本信息
- 微信公众平台开发-网页授权获取用户基本信息
- 微信平台网页授权获取用户基本信息
- 微信oauth网页授权获取用户基本信息
- 微信使用网页授权获取用户基本信息
- 微信公众号网页授权获取用户基本信息
- 微信 网页授权获取用户基本信息 (一)
- 微信开发--网页授权获取用户基本信息
- flask 微信公众号 网页授权获取用户基本信息
- 微信网页授权获取用户基本信息--PHP
- 华为机试题—0交换排序
- 【JZOJ5231】【NOIP2017模拟A组模拟8.5】序列问题
- Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次
- IO流——管道流
- maven构建项目一(搭建项目架构)
- 微信网页授权获取用户基本信息(snsapi_userinfo)
- HDU6090 Rikka with Graph
- 单片机点阵案例循环显示爱心和“亲”字
- jquery分页插件快速使用
- Ajax的详解
- Session和Cookie简介
- mysql自动断开链接的问题,org.springframework.dao.RecoverableDataAccessException
- JS身份证号码验证
- 我的淘宝天猫购物返利微信机器人,不发广告,不发骚扰信息