新浪微博第三方登陆网站

来源:互联网 发布:连杆机构设计软件 编辑:程序博客网 时间:2024/05/14 19:56

我们产品增加了第三方登陆功能,首先是新浪微博,实现功能如下:

一、要有新浪微博账号,然后申请开通开发者信息(完善用户详细信息),创建应用---新浪微博API网站有详细介绍步骤,一步步操作进行即可。

二、在登陆页面合理位置增加一个a标签,红色部分是微博登陆图标,a标签的href里面填写申请token的api地址,蓝色是上一步应用的app key,紫色是强制登陆,绿色是创建应用时填写的回调地址

<a href="https://api.weibo.com/oauth2/authorize?client_id=xxxx&forcelogin=true&response_type=code&redirect_uri=xxxx"><img src="/images/weibologo.png"></a>

三、在回调地址的方法中做如下代码

     $code=$this->getRequest()->getParam("code","");//获取code数据
    //获取token,根据跳转过来得到的code,进一步获取授权token
    $formgettoken="https://api.weibo.com/oauth2/access_token?client_id=xxxx&client_secret=xxxxx&grant_type=authorization_code&redirect_uri=xxxxxx&code=".$code;
    $patoken=array(    
    );
    $patoken=json_encode($patoken);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $formgettoken);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,0);
    curl_setopt($ch, CURLOPT_POSTFIELDS,($patoken));
    $res = curl_exec($ch);
    curl_close($ch);
    $res=json_decode($res,true);
    $actok=$res['access_token'];//获取到access_token
    $uid=$res['uid'];//登陆用户的唯一id
    //根据获取到的access_token和用户唯一id得到用户详细信息
    $formgettoken="https://api.weibo.com/2/users/show.json?source=xxxx&access_token=$actok&uid=$uid";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $formgettoken);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,0);
    $res = curl_exec($ch);
    curl_close($ch);
    $res=json_decode($res,true);
然后$res里面就是用户的全部信息,根据自己项目需要进行选择信息保存
0 0
原创粉丝点击