关于新浪微博连接

简介:这是关于新浪微博连接的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340855' scrolling='no'>

最近要做新浪微博连接,搜出以前做的,也许有的人需要但不了解,现在来说说整个流程。懂得流程就很简单了。新浪跟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");
}
?>

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/340855.html pageNo:7

posted on 2011-11-11 08:47  圣者  阅读(172)  评论(0编辑  收藏  举报

导航