<?php
session_start();

header("Content-type: text/html; charset=utf-8");
$home = 'index.php';
class db{
    private $host;
    private $user;
    private $pass;
    private $database;
    private $charset;
    function __construct($host,$user,$pass,$database,$charset){
        $this->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->database=$database;
        $this->charset=$charset;
        $this->connect();
    }
    function connect(){
        mysql_connect($this->host,$this->user,$this->pass) or die ("连接数据库服务器失败!");
        mysql_select_db($this->database) or die ("连接数据库失败!");
        mysql_query("set names $this->charset");        
    }
    function select($sql){
        $select=mysql_query($sql);
        $rows = '';
        while($row = mysql_fetch_array($select, MYSQL_ASSOC)) {
        $rows[] = $row;
        }
        return $rows;
    }
    function insert($tab,$col,$value){
        mysql_query("INSERT INTO $tab($col)values($value)");
        return mysql_insert_id();
    }
    function update($tab,$col,$new_value,$colm,$value){
        mysql_query("UPDATE $tab SET $col=$new_value where $colm=$value");    
    }
    function delete($tab,$col,$value){
      mysql_query("DELETE FROM $tab where $col=$value");
    }
    function close(){
    mysql_close();
    }
}
$db = new db('127.0.0.1','root','lizhifeng','jinba','utf8');

//通过oauth2授权获取用户详细信息
//$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx954b290311defa52&redirect_uri=http://jinba2.emailcar.net&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

if(!$_SESSION['userid']){
if (isset($_GET['code'])){
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx954b290311defa52&secret=299a1a9ba0db6d1b09266842de62079c&code='.$code.'&grant_type=authorization_code';
$json = file_get_contents($url); 
$arr = json_decode($json,true);
$token = $arr['access_token'];  
$openid = $arr['openid'];
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid.'&lang=zh_CN'; 
$json = file_get_contents($url); 
$arr = json_decode($json,true); 
$name = $arr['nickname'];
$imgURL = $arr['headimgurl'];
$sex = $arr['sex'];
$province = $arr['province'];
$city= $arr['city'];
$country= $arr['country'];
$result = $db->select('select * from member where openid = "'.$openid.'"');
if($result){
$userid = $result[0]['id'];
$_SESSION['userid'] = $userid;
}else{
if($openid){
$userid = $db->insert('member','openid,nickname,headimgurl,sex,city,country,province,create_time','"'.$openid.'","'.$name.'","'.$imgURL.'","'.$sex.'","'.$city.'","'.$country.'","'.$province.'","'.time().'"');
$_SESSION['userid'] = $userid;
}else{
header("Location: $url"); 
}
}
}else{
    header("Location: $url"); 
}
}
$userid = $userid?$userid:$_SESSION['userid'];

微信oauth2授权获得用户信息的更多相关文章

  1. php微信网页授权获取用户信息

    配置回调域名: 1. 引导用户进入授权页面同意授权,获取code 2. 通过code换取网页授权access_token(与基础支持中的access_token不同) 3. 如果需要,开发者可以刷新网 ...

  2. 微信OAuth授权获取用户OpenId-JAVA(个人经验)【申明:来源于网络】

    微信OAuth授权获取用户OpenId-JAVA(个人经验)[申明:来源于网络] 地址:https://my.oschina.net/xshuai/blog/293458

  3. 微信小程序 获取用户信息并保存登录状态

    微信小程序 获取用户信息并保存登录状态:http://www.360doc.com/content/18/0124/11/9200790_724662071.shtml

  4. Laravel wxxcx 微信小程序获取用户信息

    wxxcx 是Laravel5微信小程序登录获取用户信息扩展 部署 12345678 # 安装$ composer require iwanli/wxxcx# 注册服务# 在 /config/app. ...

  5. 微信 OAuth2 网页授权获取用户信息

    文档:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html !!! 微信跟用户没有关系类接口采用了OAUTH2 [ ...

  6. 微信 公众号 小程序 授权 unionid 用户信息 实验总结

    -*-*-*-*-*-*-*-*-*--*-*-*-1.小程序通过code获取用户openid的接口,如果用户曾经授权并未过期,或者用户关注过同主体的公众号,会带回unionID,但没有用户头像等信息 ...

  7. 微信公众号授权回调用户信息,获取openid

    1.--------------------用户授权登录并获取code 授权登录方式有两个,一种为静默授权登录(scope=snsapi_base),一种为非静默授权登录(scope=snsapi_u ...

  8. 微信网页授权获取用户openid及用户信息

    $code = $_GET["code"];//获取code $appid=“xxxx”;//公众号appid $APPSECRET="xxx";//公众号ap ...

  9. 微信小程序 获取用户信息授权

    login.wxml界面 获取用户授权可以用微信提供的接口 wx.authorize, 但是获取用户信息的授权用这个接口不会弹出授权窗口 <button class="submit-b ...

随机推荐

  1. 如何创建一个Asp .Net Web Api项目

    1.点击文件=>新建=>项目 2.创建一个Asp .NET Web项目 3.选择Empty,然后选中下面的MVC和Web Api,也可以直接选择Web Api选项,注意将身份验证设置为无身 ...

  2. 基于云开发开发 Web 应用(三):云开发相关数据调用

    介绍 在完成了 UI 界面的实现后,接下来可以开始进行和云开发相关的数据对接.完成数据对接后,应用基础就打好了,接下来的就是发布上线以及一些小的 feature 的加入. 配置 在进行相关的配置调用的 ...

  3. 089-PHP数组运用 - 通过循环函数取出部分成员合并成新数组

    <?php function myfunc($arr){ //定义过滤函数 $j=count($arr); for($i=0;$i<$j;$i++){ if($arr[$i]>=0& ...

  4. 使用HttpURLConnection方式访问接口

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  5. 【BZOJ4237】稻草人

    题意 给定平面上 \(N\) 个关键点,询问有多少个矩形满足左下和右上各有一个关键点,且矩形中间没有关键点. \(N\le 2\cdot 10^5\) . 题解 我们按 \(x\) 排序分治,对于左右 ...

  6. RFX2401C与RFX2402E的区别

    随着科技的发展,射频设备也慢慢的普及,射频放大器在射频设备中起着非常重要的作用.为了能获得足够大的距离,必须都要外加射频信号放大器. 射频信号放大器简称 “PA”.PA主流应用主要有ZigBee .无 ...

  7. bzoj1415 NOI2005聪聪和可可

    %%%http://hzwer.com/2819.html 先各种暴力搞出来p[x][y](从x到y下一个最近应该到达的位子) 然后就记忆化搜索??(雾) #include<bits/stdc+ ...

  8. springboot整合mybatis与thymeleaf

    1.创建springboot项目 (1)选择Spring Initializr (2)填写自己的Group 与 Artifact (3)选择依赖框架 等待maven下载好依赖和插件即可 2.主配置文件 ...

  9. php添加openssl扩展

    很多时候都会用到openssl组件,下面就介绍一下linux下php安装openssl扩展: 安 装openssl组件,一般php安装目录中都有许多扩展组件的安装包,当然也包括openssl,例如我的 ...

  10. hash表系列(转)

    http://www.cnblogs.com/mumuxinfei/p/4441826.html 前言: 我以前在百度的mentor, 在面试时特喜欢考察哈希表. 那时的我满是疑惑和不解, 觉得这东西 ...