About Sina Weibo connection

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page on Sina Weibo's connection. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 340855 'rolling = 'no'>

Recently I am going to create a Sina Weibo connection. I found out what I did before. Some people may need but don't know about it. Now let's talk about the entire process. It is easy to understand the process. The connection between Sina and QQ is similar.

First, go to Sina to apply for the app key and download the SDK with a demo. You can first understand.

Step 1: generate a Sina login Link
Require_once ("includes/weibooauth. php ");
// Obtain the login URL
$ O = new weibooauth (wb_akey, wb_skey );
$ Keys = $ o-> getrequesttoken ();
$ Aurl = $ o-> getauthorizeurl ($ keys ['auth _ token'], false, "{$ web ['weburl']}/sinalogin. php? Act = apilogin ");
$ _ Session ['keys '] = $ keys;
$ Smarty-> assign ("sinaurl", $ aurl );
$ Smarty-> display ("user_login.html ");

Step 2: Return parameter processing after Sina Login
Determine whether the Sina account on this site exists. If the account does not exist, generate the account. If the account exists, go to step 3.
// Process user data on this site
$ C = new weiboclient (wb_akey, wb_skey, $ _ session ['last _ key'] ['oss _ token'], $ _ session ['last _ key'] ['oss _ token_secret ']);

$ Xuser = $ C-> show_user ($ _ session ['last _ key'] ['user _ id']);
// Convert string Encoding
$ Xuser = iconvstr ("UTF-8", "GBK", $ xuser );
$ Xs = $ db-> getrow ("select * from ". table ('userapi '). "Where xuserid = ". $ xuser ['id']. "And xfrom = 'sina '");
// Records exist and bound
If ($ Xs ['bind'] = 1)
{
// Directly log on if it has been bound
Header ("Location: sinalogin. php? Act = Done ");
} Else
{
If (! $ XS)
{
// Insert data if it does not exist
$ Db-> query ("insert ". table ('userapi '). "Set xuserid = ". $ xuser ['id']. ", xusername = '". $ xuser ['name']. "', xfrom = 'sina '");

}

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

// Process login options
$ Smarty-> display ("sina_login.html ");
}

Step 3: Check whether the local account is bound!

// Last step of login
Header ("Content-Type: text/html; charset = gb2312 ");
$ C = new weiboclient (wb_akey, wb_skey, $ _ session ['last _ key'] ['oss _ token'], $ _ session ['last _ key'] ['oss _ token_secret ']);
$ Xuser = $ C-> show_user ($ _ session ['last _ key'] ['user _ id']);
// Convert string Encoding
$ Xuser = iconvstr ("UTF-8", "GBK", $ xuser );
// If you have an account, bind it to the account
If ($ _ Get ['a'] = 'bind ')
{
If (TRIM ($ _ post ['yzm'])! = $ _ Session ['code']) errback ('verification code error ');
$ Username = trim (strip_tags ($ _ post ['username']);
Empty ($ username) & errback ('user name cannot be blank ');
$ Password = MD5 (TRIM ($ _ post ['Password']);
$ Userid = $ db-> getone ("select userid from". Table ('user'). "Where username = '$ username' and Password =' $ password '");
If (! $ Userid) errback ('user name or Password error, binding failed ');
$ 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 '");
// If no UID is associated, generate
If (! $ Xu)
{
// If not, an account is bound.
$ 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 ". 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 '");
}
}
// Finally select the user
$ 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 ("des/init. php ");
Require_once ("includes/weibooauth. php ");

$ Act = trim ($ _ request ['ac']);
If (empty ($ Act ))
{
$ Act = "Index ";
}

If ($ act = 'index '){
Require_once ("includes/weibooauth. php ");
// Obtain the login URL
$ O = new weibooauth (wb_akey, wb_skey );
$ Keys = $ o-> getrequesttoken ();
$ Aurl = $ o-> getauthorizeurl ($ keys ['auth _ token'], false, "{$ web ['weburl']}/sinalogin. php? Act = apilogin ");
$ _ Session ['keys '] = $ keys;
$ Smarty-> assign ("sinaurl", $ aurl );
$ Smarty-> display ("user_login.html ");
} Else ($ act = 'apilogin ')
{
// Process the login data on Sina
// Obtain encrypted data
$ O = new weibooauth (wb_akey, wb_skey, $ _ session ['keys '] ['oss _ token'], $ _ session ['keys '] ['oss _ token_secret']);

$ Last_key = $ o-> getaccesstoken ($ _ request ['oss _ verifier ']);

$ _ Session ['last _ key'] = $ last_key;

Header ("Location: sinalogin. php? Act = show ");
} Elseif ($ act = "show ")
{
// Process user data on this site
$ C = new weiboclient (wb_akey, wb_skey, $ _ session ['last _ key'] ['oss _ token'], $ _ session ['last _ key'] ['oss _ token_secret ']);

$ Xuser = $ C-> show_user ($ _ session ['last _ key'] ['user _ id']);
// Convert string Encoding
$ Xuser = iconvstr ("UTF-8", "GBK", $ xuser );
$ Xs = $ db-> getrow ("select * from ". table ('userapi '). "Where xuserid = ". $ xuser ['id']. "And xfrom = 'sina '");
// Records exist and bound
If ($ Xs ['bind'] = 1)
{
// Directly log on if it has been bound
Header ("Location: sinalogin. php? Act = Done ");
} Else
{
If (! $ XS)
{
// Insert data if it does not exist
$ Db-> query ("insert ". table ('userapi '). "Set xuserid = ". $ xuser ['id']. ", xusername = '". $ xuser ['name']. "', xfrom = 'sina '");

}

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

// Process login options
$ Smarty-> display ("sina_login.html ");
}
} Elseif ($ act = "done ")
{// Last step of login
Header ("Content-Type: text/html; charset = gb2312 ");
$ C = new weiboclient (wb_akey, wb_skey, $ _ session ['last _ key'] ['oss _ token'], $ _ session ['last _ key'] ['oss _ token_secret ']);
$ Xuser = $ C-> show_user ($ _ session ['last _ key'] ['user _ id']);
// Convert string Encoding
$ Xuser = iconvstr ("UTF-8", "GBK", $ xuser );
// If you have an account, bind it to the account
If ($ _ Get ['a'] = 'bind ')
{
If (TRIM ($ _ post ['yzm'])! = $ _ Session ['code']) errback ('verification code error ');
$ Username = trim (strip_tags ($ _ post ['username']);
Empty ($ username) & errback ('user name cannot be blank ');
$ Password = MD5 (TRIM ($ _ post ['Password']);
$ Userid = $ db-> getone ("select userid from". Table ('user'). "Where username = '$ username' and Password =' $ password '");
If (! $ Userid) errback ('user name or Password error, binding failed ');
$ 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 '");
// If no UID is associated, generate
If (! $ Xu)
{
// If not, an account is bound.
$ 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 ". 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 '");
}
}
// Finally select the user
$ 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 ");
}
?>

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/340855.html pageno: 7.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.