在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站。这里以微博登录为例。微博登录包括身份认证、用户关系以及内容传播。允许用户使用微博帐号登录访问第三方网站,分享内容,同步信息。
1、首先需要引导需要授权的用户到如下地址:
https://api.weibo.com/oauth2/authorize"htmlcode">
{ "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 = "***微博应用安全域名***/"; //授权后将页面重定向到本地项目 $redirect_uri = urlencode($redirect_uri); $wb_url = "https://api.weibo.com/oauth2/authorize"; $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"; $token = post($url, array()); $token = json_decode($token, true); //获取用户信息 : get方法,替换参数: access_token, uid $url = "https://api.weibo.com/2/users/show.json"; $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>效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]
- 明达年度发烧碟MasterSuperiorAudiophile2017[DSF]
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WAV+CUE】
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]