Recently, my website wanted to add a Sina Weibo login feature. I searched online and downloaded a plugin, but it didn’t work on my site. So I decided to try it myself. As a PHP beginner, I hope the experts can offer their guidance.
1. First, go to http://open.weibo.com/ to apply for an app
2. Download the latest plugin package from http://code.google.com/p/libweibo/downloads/list
2.1 You need to modify the config.php file
define( "WB_AKEY" , 'the key you applied for ); define( "WB_SKEY" , 'the corresponding App Secret' ); define( "WB_CALLBACK_URL" , 'http://domain/login/callback.php' );
2.2 Modify the callback.php file, replace all the code with the following
session_start();define('IN_ECS', true);require('../includes/init.php');include_once('../includes/lib_transaction.php');include_once('../includes/lib_passport.php');include_once( 'config.php' );include_once( 'saetv2.ex.class.php' );$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );if (isset($_REQUEST['code'])) { $keys = array(); $keys['code'] = $_REQUEST['code']; $keys['redirect_uri'] = WB_CALLBACK_URL; try { $token = $o->getAccessToken( 'code', $keys ) ; } catch (OAuthException $e) { }}if ($token) { $_SESSION['token'] = $token; setcookie( 'weibojs_'.$o->client_id, http_build_query($token) ); $c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );$ms = $c->home_timeline(); // done$uid_get = $c->get_uid();$uid = $uid_get['uid'];$user_message = $c->show_user_by_id( $uid);// Get basic user info by IDfunction check_user($username){$sql = "SELECT user_id, password, salt " . " FROM " . $GLOBALS['ecs']->table("users"). " WHERE user_name='$username'"; $row = $GLOBALS['db']->getRow($sql); if (!empty($row)){ return true; }else{ return false; }}if($user_message['screen_name']!==""){ // Get Weibo nickname $username=$user_message['screen_name']; $password=time();// Random password, not used anyway $email='[email protected]';// Default email placeholder $back_act ="/user.php"; if (check_user($username)!==false){ $GLOBALS['user']->set_session($username); $GLOBALS['user']->set_cookie($username); header("Location: /user.php/n"); // Auth success, redirect exit; }else{ $reg_date = time(); $password =md5($password); $ip=real_ip(); $GLOBALS['db']->query('INSERT INTO ' . $GLOBALS['ecs']->table("users") . "(`email`, `user_name`, `password`, `reg_time`, `last_login`, `last_ip`) VALUES ('$email', '$username', '$password', '$reg_date', '$reg_date', '$ip')"); $GLOBALS['user']->set_session($username); $GLOBALS['user']->set_cookie($username); header("Location: /user.php/n"); exit; }}else{ echo 'fail'; exit; }?> } else {?>Authorization failed.}?>
These are the only two files that need modification. The weibolist.php file can be deleted.
3. Then upload the four files — config.php, callback.php, index.php, and saetv2.ex.class.php — to the login folder on your server.
That’s basically it — the functionality is complete. I hope this helps fellow webmasters who need it.
Demo site shared by the author: http://www.diy520.cn
http://www.3lian.com/edu/2012/09-11/36501.html