微信授权

授权示意图

1.第一个访问的页面index.html

//用一个button进行链接的访问
<button onclick="btn()">点击微信登录</button>
<script type="text/javascript">
        function btn(){
            window.location.href ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2c46f71ebdd03f90&redirect_uri=http://wx.littlewindy.com/get_quan.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        }
    </script>

2.get_quan.php

由第一步代码可知页面跳转到下面的页面
http://wx.littlewindy.com/get_quan.php


<?php
    session_start();
    header("Content-type: text/html; charset=utf-8"); 
    require_once "DataBase.php";
    require_once "wxAPI.php";

    $appID = "";//填你自己的数据吧
    $appSecret = "";//填你自己的数据吧
    //获取accesstoken
    $getAccessURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appID}&secret={$appSecret}&code={$_GET['code']}&grant_type=authorization_code";

    $accessStr = httpGet($getAccessURL);

    $accObj = json_decode($accessStr,true);//关联数组
    //获取用户信息
    $getInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token={$accObj['access_token']}&openid={$accObj['openid']}&lang=zh_CN";

    $reStr = httpGet($getInfoURL);

    $reObj = json_decode($reStr, true);//关联数组
    //print_r($reObj);

    $openid = $reObj['openid'];
    $nickname = $reObj['nickname'];
    $sex = $reObj['sex'];
    $city = $reObj['city'];
    $province = $reObj['province'];
    $country = $reObj['country'];

    /*传递的Session值 给后面 需要往数据库里 填充数据的页面*/
    $_SESSION['openid'] = $openid;
    $_SESSION['nickname'] = $nickname;

    $db = new DB("localhost","root","数据库密码","wx");
    //查看用户信息,如果用户不存在,则插入信息,如果存在不操作
    $select = $db->selSQL("select * from user where openid = '$openid'");
    if($select){
        echo "user is existed";
    }else{
        $insert = $db->iduSQL("insert into user (openid,nickname,sex,city,province,country) values ('$openid','$nickname','$sex','$city','$province','$country')");
        echo "success for your insert";
    }

    header("Location: http://wx.littlewindy.com/fly/index.html"); 

?>

3.封装好的数据库DataBase.php

<?php
    class DB{
        // 链接并选择数据库
        function __construct($ip = "", $dataN = "", $dataP = "", $dbName = ""){
          $con = mysql_connect($ip, $dataN, $dataP) or die(mysql_error());
          mysql_select_db($dbName, $con) or die(mysql_error());
          mysql_query("set names utf8");
        }
        // 查询的SQL语句方法
        function selSQL($sqlStr){
            $result = mysql_query($sqlStr) or die(mysql_error());
            $arr = array();
            while($row = mysql_fetch_assoc($result)){
                array_push($arr, $row);
            }
            return $arr;
        }
        // 插入/删除/更新的方法
        function iduSQL($sql){
            mysql_query($sql);
            // mysql_affected_rows() 得到受影响的函数
            if (mysql_affected_rows() > 0){
                return true;
            } else {
                return false;
            }
        }
    }

?>

4.wxAPI.php

<?php
// get请求方式抓包
function httpGet($url) {
    // http://blog.sina.com.cn/s/blog_640738130100tsig.html
    // 初始化对象
    $curl = curl_init();
    // curl_setopt() 设置会话参数
    // CURLOPT_RETURNTRANSFER 结果保存到字符串中
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    // CURLOPT_TIMEOUT 超时秒数
    curl_setopt($curl, CURLOPT_TIMEOUT, 100);
    // 注意:
    // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查  ;true:从证书中检查SSL加密算法是否存在 
    curl_setopt($curl, CURLOPT_URL, $url);

    // 执行会话
    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}


// 通过POST请求的网址, 抓取上面的内容
function httpPost($data,$url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    return $tmpInfo;
}




?>

完整游戏代码

完整游戏代码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值