TP5.1整合微博第三方登陆类[概要],与上期的QQ登陆差不多

php编程 4个月前(2019-06-25 22:57) 晓峰哥 463 5

第三方登陆插件,网上也有一大堆,我是借用以前项目文件中的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

token url curl false param

分享到




评论列表(共条)

关于我(About Me)

美男子(晓峰哥)

欢迎来到我的世界,有你会更精彩^@^

在这个纷繁的世界里,放下而又拿起,是多么的不容易.