Using PHP to call Weibo interface to realize Weibo login
蘑菇宝
蘑菇宝 2018-12-03 15:40:22
0
0
1193

During the normal project development process, in addition to registering an account on this website to log in, you can also call a third-party interface to log in to the website. Here we take Weibo login as an example. Weibo login includes identity authentication, user relations and content dissemination. Allow users to log in with a Weibo account to access third-party websites, share content, and synchronize information.

1. First, you need to guide users who need authorization to the following address:

https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI


If the user agrees to authorization, the page jumps to YOUR_REGISTERED_REDIRECT_URI/?code=CODE :

2. Next, you need to exchange the Access Token based on the code obtained above:

https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE

Return value: JSON

{ "access_token": "SlAV32hkKG", "remind_in": 3600, "expires_in": 3600 }


3 . Finally, use the obtained OAuth2.0 Access Token to call the API, obtain the user's identity, and complete the user's login.

For convenience, we first encapsulate get and post into common.php under application:

Application public file common.php:

function get( $url, $_header = NULL ){    
$curl = curl_init();    
//curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);     
if( stripos($url, 'https://') !==FALSE )    
{        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);    }    curl_setopt($curl, CURLOPT_URL, $url);    curl_setopt($curl, CURLOPT_HEADER, 0);    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    if ( $_header != NULL )    {        curl_setopt($curl, CURLOPT_HTTPHEADER, $_header);    }    $ret    = curl_exec($curl);    $info    = curl_getinfo($curl);    curl_close($curl);    if( intval( $info["http_code"] ) == 200 )    {        return $ret;    }    return false;}/* * post method */function post( $url, $param ){     $oCurl = curl_init ();    curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false);    if (stripos ( $url, "https://" ) !== FALSE) {        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );    }    curl_setopt ( $oCurl, CURLOPT_URL, $url );    curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );    curl_setopt ( $oCurl, CURLOPT_POST, true );    curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $param );    $sContent = curl_exec ( $oCurl );    $aStatus = curl_getinfo ( $oCurl );    curl_close ( $oCurl );    if (intval ( $aStatus ["http_code"] ) == 200) {        return $sContent;    } else {        return false;    }}

Controller Processing code Login.php:

class Login extends \think\Controller 
{
    public function index()
    {
        $key = "****";
        $redirect_uri = "***微博应用安全域名***/?backurl=***项目本地域名***/home/login/webLogin?";
        //授权后将页面重定向到本地项目
        $redirect_uri = urlencode($redirect_uri);
        $wb_url = "https://api.weibo.com/oauth2/authorize?client_id={$key}&response_type=code&redirect_uri={$redirect_uri}";
        $this -> assign('wb_url',$wb_url);
        return view('login');
    }
    public function webLogin(){
        $key = "*****";
        //接收code值
        $code = input('get.code');
        //换取Access Token: post方式请求    替换参数: client_id, client_secret,redirect_uri, code
        $secret = "********";
        $redirect_uri = "********";
        $url = "https://api.weibo.com/oauth2/access_token?client_id={$key}&client_secret={$secret}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
        $token = post($url, array());
        $token = json_decode($token, true);
        //获取用户信息 : get方法,替换参数: access_token, uid
        $url = "https://api.weibo.com/2/users/show.json?access_token={$token['access_token']}&uid={$token['uid']}";
        $info = get($url);
        if($info){
            echo "<p>登录成功</p>";
        }
    }
}


Template code login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博登录</title>
</head>
<body>
<a href="{$wb_url}">点击这里进行微博登录</a>
</body>
</html>
蘑菇宝
蘑菇宝

精品PHP中高级进阶学习教程,需要加微信:PHPopen888,还可加入微信群,分享tp,laravel,swoole等...

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template