25 06 2019
第三方登陆插件,网上也有一大堆,我是借用以前项目文件中的WeiboConnect.class.php进行的改造,比用微博的SDK方便得多,希望对大家有用,不多说,直接上代码

一句话简单介绍,改session,改请求方式get,post,其他的请求方式在WeiboConnect.php里面已有,代码如下

<?php
namespace app\common\Third;
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Copyright (c) 2018 xf.fengzhiya.cn.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: @ Xf 改
// +----------------------------------------------------------------------

class WeiboConnect {

    private function get_access_token($appkey, $appsecretkey, $code, $callback, $state=null) {
        $url = "https://api.weibo.com/oauth2/access_token";
        $param = array(
            "grant_type"    =>    "authorization_code",
            "client_id"     =>    $appkey,
            "client_secret" =>    $appsecretkey,
            "code"          =>    $code,
            "redirect_uri"  =>    $callback
        );

        $param = http_build_query($param);
        $response = $this->post_url($url, $param);
        if($response == false) {
            return false;
        }
        $params = json_decode($response, true);
        return $params["access_token"];
    }
    
    private  function get_url($url, $param = null) {
        if($param != null) {
            $query = http_build_query($param);
            $url = $url . '?' . $query;
        }
        $ch = curl_init();
        if(stripos($url, "https://") !== false){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $content = curl_exec($ch);
        $status = curl_getinfo($ch);
        curl_close($ch);
        if(intval($status["http_code"]) == 200) {
            return $content;
        }else{
            echo $status["http_code"];
            return false;
        }
    }

    

    /*

     * HTTP POST Request

    */

    private  function post_url($url, $params) {
        $ch = curl_init();
        if(stripos($url, "https://") !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $content = curl_exec($ch);
        $status = curl_getinfo($ch);
        curl_close($ch);
        if(intval($status["http_code"]) == 200) {
            return $content;
        } else {
            return false;
        }
    }

    private function get_openid($access_token) {
        $url = "https://api.weibo.com/oauth2/get_token_info"; 
        $param = array(
            "access_token"    => $access_token
        );

        $param = http_build_query($param);
        $response  = $this->post_url($url, $param);
        if($response == false) {
            return false;
        }
        $params = json_decode($response, true);
        return $params['uid'];
    }

    public function get_user_info($token, $openid, $appkey=null, $format = "json") {
        $url = "https://api.weibo.com/2/users/show.json";
        $param = array(
            "access_token"      =>    $token,
            "uid"               =>    $openid
        );

        $response =$this->get_url($url, $param);
        if($response == false) {
            return false;
        }

        $user = json_decode($response, true);
        return $user;
    }

    public function login($appkey, $callback, $scope='') {
        $login_url = "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=" 
            . $appkey . "&scope=$scope&redirect_uri=" . urlencode($callback);
        header('Location:'.$login_url);
    }

    public function callback($appkey, $appsecretkey, $callback) {
        $code = input('code');
        $token = $this->get_access_token($appkey, $appsecretkey, $code, $callback);
        $openid = $this->get_openid($token);
        if(!$token || !$openid) {
            exit('get token or openid error!');
        }

        return array('openid' => $openid, 'token' => $token);
    }
}

 

*使用方法,如下图

1.从前端进入的时候参照上期的QQ第三方登陆,这里只贴图 前端地址http://you domain/index/auth/third/type/weibo.html

2.新浪后台请求的回调 回调地址http://your domain/index/auth/index/type/weibo.html

 

全部发表完毕 有疑问可以在文章下面给我留言或者加我为好友私聊我crying

延伸阅读
  1. 风之涯Chat 聊天群tp5.1实现 包含workerman实现WebSocket[2019-10-19 16:28:39]
  2. TP5.1实现邮件发送功能[简单][2019-07-16 19:40:01]
  3. PHP读取doc-docx-xls-pdf-txt内容[2019-07-12 22:18:37]
  4. TP5.1整合 QQ第三方登陆类[全过程][2019-06-24 22:06:27]
  5. ThinkPHP5.1你一定要引起重视的几个细节[2019-06-23 13:42:09]
  6. PHP生成随机数的几种方法[2019-06-22 02:43:46]
发表评论