PHP调用微博接口实现微博登录的方法示例
程序员文章站
2023-11-12 19:11:22
在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站。这里以微博登录为例。微博登录包括身份认证、用户关系以及内容传播。允许用户使用微博帐号...
在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站。这里以微博登录为例。微博登录包括身份认证、用户关系以及内容传播。允许用户使用微博帐号登录访问第三方网站,分享内容,同步信息。
1、首先需要引导需要授权的用户到如下地址:
https://api.weibo.com/oauth2/authorize?client_id=your_client_id&response_type=code&redirect_uri=your_registered_redirect_uri
如果用户同意授权,页面跳转至 your_registered_redirect_uri/?code=code:
2、接下来要根据上面得到的code来换取access token:
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
返回值:
json
{ "access_token": "slav32hkkg", "remind_in": 3600, "expires_in": 3600 }
3、最后,使用获得的oauth2.0 access token调用api,获取用户身份,完成用户的登录。
话不多说,直接上代码:
为了方便,我们先将get和post封装到application下的common.php中:
应用公共文件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; } }
控制器处理代码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>"; } } }
模板代码login.html:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>微博登录</title> </head> <body> <a href="{$wb_url}" rel="external nofollow" >点击这里进行微博登录</a> </body> </html>
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。