首页 > 作文

PHP调用微博接口实现微博登录的方法示例

更新时间:2023-04-06 17:09:07 阅读: 评论:0

在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站。这里以微博登录为例。微博登录包括身份认证、用户关系以及内容传播。允许用户使用微博帐号登录访问第三方网站,分享内容,同步信息。

1、首先需要引导需要授权的用户到如下地址:

/d/file/titlepic/authorize your_registered_redirect_uri/?code=code:

2、接下来要根据上面得到的code来换取access token:

https://api.weibo.com/oauth2/access_token?client_id=your_client_id&client_cret=your_client_cret&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, $_heade丁达尔效应r = null ){  $curl = curl_init();  //curl_topt ( $curl, curlopt_safe_upload, fal);   if( stripos($url, 'https://') !==fal )  {    curl写秋天的四字词语_topt($curl, curlopt_ssl_verifypeer, fal);    curl_topt($curl, curlopt_ssl_verifyhost, fal);  }  curl_topt($curl, curlopt_url, $url);  curl_topt($curl, curlopt_header, 0);  curl_topt($curl, curlopt_returntransfer, 1);  if ( $_header != null )  {    curl_topt($curl, curlopt_httpheader, $_header);  }  $ret  = curl_exec($curl);  $info  = curl_getinfo($curl);  curl_clo($curl);  if( intval( $info["http_code"] ) == 200 )  {    return $ret;  }  return fal;}/* * pos300字t method */function post( $url, $param ){   $ocurl = curl_init ();  curl_topt ( $ocurl, curlopt_safe_upload, fal心理测试题及答案);  if (stripos ( $url, "https://" ) !== fal) {    curl_topt ( $ocurl, curlopt_ssl_verifypeer, fal );    curl_topt ( $ocurl, curlopt_ssl_verifyhost, fal );  }    curl_topt ( $ocurl, curlopt_url, $url );  curl_topt ( $ocurl, curlopt_returntransfe法治国家是法治建设的r, 1 );  curl_topt ( $ocurl, curlopt_post, true );  curl_topt ( $ocurl, curlopt_postfields, $param );  $scontent = curl_exec ( $ocurl );  $astatus = curl_getinfo ( $ocurl );  curl_clo ( $ocurl );  if (intval ( $astatus ["http_code"] ) == 200) {    return $scontent;  } el {    return fal;  }}

控制器处理代码login.php:

class login extends \think\controller {  public function index()  {    $key = "****";    $redirect_uri = "***微博应用安全域名***/?backurl=***项目本地域名***/home/login/weblogin?";    //授权后将页面重定向到本地项目    $redirect_uri = urlencode($redirect_uri);    $wb_url = "/d/file/titlepic/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_cret,redirect_uri, code    $cret = "********";    $redirect_uri = "********";    $url = "https://api.weibo.com/oauth2/access_token?client_id={$key}&client_cret={$cret}&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/urs/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 chart="utf-8">  <title>微博登录</title></head><body><a href="{$wb_url}" rel="external nofollow" >点击这里进行微博登录</a></body></html>

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-06 17:09:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/213dc1aa73cf4d60044723ef61386620.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP调用微博接口实现微博登录的方法示例.doc

本文 PDF 下载地址:PHP调用微博接口实现微博登录的方法示例.pdf

标签:用户   代码   项目   第三方
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图