• 关于新浪微博连接


    最近要做新浪微博连接,搜出以前做的,也许有的人需要但不了解,现在来说说整个流程。懂得流程就很简单了。新浪跟qq的连接都类似。

    首先要去新浪申请app key,下载sdk 里面还有demo ,可以先了解下。

    第一步:生成新浪登录链接
    require_once("includes/weibooauth.php");
    //获取登陆url
    $o=new WeiboOAuth(WB_AKEY,WB_SKEY);
    $keys=$o->getRequestToken();
    $aurl=$o->getAuthorizeURL($keys['oauth_token'],false,"{$web['weburl']}/sinalogin.php?act=apilogin");
    $_SESSION['keys'] = $keys;
    $smarty->assign("sinaurl",$aurl);
    $smarty->display("user_login.html"); 

    第二步:在新浪登录后返回参数处理
    判断是否存在本站 新浪账号,不存在则生成,存在则进入第三步
    //处理用户数据 本站端
    $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );

    $xuser=$c->show_user($_SESSION['last_key']['user_id']);
    //转化字符串编码
    $xuser=iconvstr("utf-8","gbk",$xuser);
    $xs=$db->getRow("select * from  ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina' ");
    //存在记录 且已经绑定
     if($xs['bind']==1)
     {
      //如果已经绑定 则直接登陆
      header("Location: sinalogin.php?act=done"); 
     }else
     {
      if(!$xs)
      {
      //如果不存在则插入数据  
      $db->query("insert into  ".table('userapi')." set xuserid=".$xuser['id'].",xusername='".$xuser['name']."',xfrom='sina' ");
      
      }
      
      $smarty->assign("user",$xuser);
      
      //处理登陆选项
      $smarty->display("sina_login.html");
     }

    第三步:处理是否绑定本地账号,完成!

    //登陆最后一步
     header("Content-type:text/html;charset=gb2312");
     $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
     $xuser=$c->show_user($_SESSION['last_key']['user_id']);
     //转化字符串编码
     $xuser=iconvstr("utf-8","gbk",$xuser);
     //如果有账号则绑定账号
     if($_GET['a']=='bind')
     {
      if(trim($_POST['yzm'])!=$_SESSION['code']) errback('验证码错误');
      $username=trim(strip_tags($_POST['username']));
      empty($username) && errback('用户名不能为空');
      $password=md5(trim($_POST['password']));
      $userid=$db->getOne("select userid from ".table('user')." where username='$username' and password='$password' ");
      if(!$userid) errback('用户名或者密码出错,绑定不成功');
      $db->query("update ".table('userapi')." set uid='$userid',bind=1 where xuserid=".$xuser['id']." and xfrom='sina' ");   
     }elseif($_GET['a']=='newbind')
     {
      $xu=$db->getOne("select uid from ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina'  ");
     //如果没有关联uid,则生成
      if(!$xu)
      {
       //如果没有则 生成一个账号 绑定
       $tempname=$username=$xuser['name'];
       $i=1;
       $j=0;
       while($i)
       {
        
        $i=$db->getOne("select count(*) from ".table('user')." where username='$tempname' ");
        if($i>0)
        {
        $tempname=$username.$j;
        $j++;
        }
       }
       $username.=$j?$j:"";
       $db->query("insert into ".table('user')." set username='$username',nickname='".$xuser['name']."' "); 
       $userid=$db->insert_id();
       $db->query("update ".table('userapi')." set uid='$userid' where xuserid=".$xuser['id']." and xfrom='sina' ");
     }
     }
     //最后选出用户
     $uid=$db->getOne("select uid from ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina'  ");
     $_SESSION['ss_userid']=$uid;
     $_SESSION['ss_username']=$xuser['name'];
     header("Location: index.php");

    <?php
    session_start();
    define("CT",1);
    require_once("includes/init.php");
    require_once("includes/weibooauth.php");

    $act=trim($_REQUEST['act']);
    if(empty($act))
    {
    $act="index";
    }

    if($act=='index'){
    require_once("includes/weibooauth.php");
    //获取登陆url
    $o=new WeiboOAuth(WB_AKEY,WB_SKEY);
    $keys=$o->getRequestToken();
    $aurl=$o->getAuthorizeURL($keys['oauth_token'],false,"{$web['weburl']}/sinalogin.php?act=apilogin");
    $_SESSION['keys'] = $keys;
    $smarty->assign("sinaurl",$aurl);
    $smarty->display("user_login.html"); 
    }else($act=='apilogin')
    {
    //处理登陆数据 新浪端
    //获取加密数据

    $o = new WeiboOAuth( WB_AKEY , WB_SKEY , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret'] );

    $last_key = $o->getAccessToken( $_REQUEST['oauth_verifier'] ) ;

    $_SESSION['last_key'] = $last_key;

    header("Location: sinalogin.php?act=show");
    }
    elseif($act=="show")
    {
    //处理用户数据 本站端
    $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );

    $xuser=$c->show_user($_SESSION['last_key']['user_id']);
    //转化字符串编码
    $xuser=iconvstr("utf-8","gbk",$xuser);
    $xs=$db->getRow("select * from ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina' ");
    //存在记录 且已经绑定
    if($xs['bind']==1)
    {
    //如果已经绑定 则直接登陆
    header("Location: sinalogin.php?act=done");
    }
    else
    {
    if(!$xs)
    {
    //如果不存在则插入数据
    $db->query("insert into ".table('userapi')." set xuserid=".$xuser['id'].",xusername='".$xuser['name']."',xfrom='sina' ");

    }

    $smarty->assign("user",$xuser);

    //处理登陆选项
    $smarty->display("sina_login.html");
    }
    }
    elseif($act=="done")
    {
    //登陆最后一步
    header("Content-type:text/html;charset=gb2312");
    $c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
    $xuser=$c->show_user($_SESSION['last_key']['user_id']);
    //转化字符串编码
    $xuser=iconvstr("utf-8","gbk",$xuser);
    //如果有账号则绑定账号
    if($_GET['a']=='bind')
    {
    if(trim($_POST['yzm'])!=$_SESSION['code']) errback('验证码错误');
    $username=trim(strip_tags($_POST['username']));
    empty($username) && errback('用户名不能为空');
    $password=md5(trim($_POST['password']));
    $userid=$db->getOne("select userid from ".table('user')." where username='$username' and password='$password' ");
    if(!$userid) errback('用户名或者密码出错,绑定不成功');
    $db->query("update ".table('userapi')." set uid='$userid',bind=1 where xuserid=".$xuser['id']." and xfrom='sina' ");
    }
    elseif($_GET['a']=='newbind')
    {
    $xu=$db->getOne("select uid from ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina' ");
    //如果没有关联uid,则生成
    if(!$xu)
    {
    //如果没有则 生成一个账号 绑定
    $tempname=$username=$xuser['name'];
    $i=1;
    $j=0;
    while($i)
    {

    $i=$db->getOne("select count(*) from ".table('user')." where username='$tempname' ");
    if($i>0)
    {
    $tempname=$username.$j;
    $j++;
    }
    }
    $username.=$j?$j:"";
    $db->query("insert into ".table('user')." set username='$username',nickname='".$xuser['name']."' ");
    $userid=$db->insert_id();
    $db->query("update ".table('userapi')." set uid='$userid' where xuserid=".$xuser['id']." and xfrom='sina' ");
    }
    }
    //最后选出用户
    $uid=$db->getOne("select uid from ".table('userapi')." where xuserid=".$xuser['id']." and xfrom='sina' ");
    $_SESSION['ss_userid']=$uid;
    $_SESSION['ss_username']=$xuser['name'];
    header("Location: index.php");
    }
    ?>
  • 相关阅读:
    js去掉字符串前后空格三种方法及最佳方案
    javascript笔记:Date对象及操作方法
    高性能网站建设指南总结
    javascript之词法作用域及函数的运行过程
    LETTers比赛第四场N!
    LETTers比赛第三场 1003 大明A+B解题报告
    LETTers比赛第三场 1004 Max Sum Plus Plus 解题报告
    LETTers比赛第三场 1002 Ignatius and the Princess III解题报告
    LETTers第五场Sleeping 解题报告
    LETTers比赛第四场N!的最高位
  • 原文地址:https://www.cnblogs.com/lrjxgl/p/2113592.html
Copyright © 2020-2023  润新知