zoukankan      html  css  js  c++  java
  • 微信授权


    //微信授权
    public function getOpenid()
    {

    $openid = Session('openid');
    if (!$openid) {
    if (!isset($_GET['code'])) {
    $url = $this->getAuthorizeUrl();
    Header("Location: $url");
    exit();
    } else {
    $state = Session('state');
    if ($state == $_GET['state']) {

    $code=$_GET['code'];

    $token = $this->getAccessToken($code);
    $access_token=$token['access_token'];
    $open=$token['openid'];
    $acctoken="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$open&lang=zh_CN";
    $userinfo = $this->httpsRequest($acctoken);
    $info = json_decode($userinfo, true);
    $openid = isset($token['openid']) ? $token['openid'] : '';
    if ($openid) {
    Session("openid", $openid);
    }
    }
    }
    }
    return $info;
    }
    public function getAuthorizeUrl()
    {
    $redirect_uri ="http://lk.a40.com.cn/api.php/api/login";

    $redirect_uri = urlencode($redirect_uri);

    $state =rand(1000,9999);
    Session('state', $state);
    return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe040024d0d8d6c1e&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
    }

    public function getAccessToken($code = '')
    {

    $ret ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxe040024d0d8d6c1e&secret=4ce2185589f1a7e0906341d10f8f42b4&code=$code&grant_type=authorization_code";
    $token_info = $this->httpsRequest($ret);
    $token_info = json_decode($token_info, true);
    return $token_info;
    }
    public function httpsRequest($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
    }


  • 相关阅读:
    javascript 设计模式-----观察者模式
    javascript 设计模式-----工厂模式
    javascript 设计模式-----模块模式
    javascript 设计模式-----享元模式
    javascript 设计模式-----策略模式
    js操作Dom的一些方法简化
    Centos7下不删除python2.x的情况下安装python3.x
    解决:Linux SSH Secure Shell(ssh) 超时断开的解决方法
    shell变量常用方法
    apache目录别名
  • 原文地址:https://www.cnblogs.com/maohongli/p/13366975.html
Copyright © 2011-2022 走看看