近網(wǎng)站想添加新浪微博登錄功能,于是在網(wǎng)上搜索了下,下載了一個(gè)插件的放我網(wǎng)站里面沒(méi)效果,所以覺(jué)定自己動(dòng)手試試,本人php新手說(shuō)得不到之處還望大俠高手們多多指教。
1.先去http://open.weibo.com/ 這里申請(qǐng)app
2.下載最新http://code.google.com/p/libweibo/downloads/list插件包
2.1其中需要修改config.php文件
define( "WB_AKEY" , '申請(qǐng)的到的key );
define( "WB_SKEY" , '相對(duì)應(yīng)的App Secret' );
define( "WB_CALLBACK_URL" , 'http://域名/login/callback.php' );
2.2 修改文件callback.php ,將此處代碼復(fù)制全部替換
<?php
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);//根據(jù)ID獲取用戶等基本信息
function 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']!==""){ //獲取微薄昵稱
$username=$user_message['screen_name'];
$password=time();//隨便弄個(gè)密碼 反正沒(méi)有用
$email='123456@163.com';//隨便默認(rèn)個(gè)郵箱
$back_act ="/user.php";
if (check_user($username)!==false){
$GLOBALS['user']->set_session($username);
$GLOBALS['user']->set_cookie($username);
header("Location: /user.phpn"); //驗(yàn)證成功,跳轉(zhuǎn)頁(yè)面
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.phpn");
exit;
}
}else{
echo 'fail';
exit;
}
?>
<?php
} else {
?>
授權(quán)失敗。
<?php
}
?>
修改的就這兩文件,其中weibolist.php可以刪除。
3.然后將config.php、callback.php、index.php、saetv2.ex.class.php四個(gè)文件放login文件夾上傳空間就行了。
到這基本就完成了功能了,希望對(duì)有需要的站長(zhǎng)有所幫助。
更多信息請(qǐng)查看IT技術(shù)專欄