-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.php
69 lines (59 loc) · 2.1 KB
/
callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
session_start();
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 ) ;
//$secret = $o->getAccessTokenSecret( 'secret', $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);//根据ID获取用户等基本信息
$uname = $user_message['screen_name'];
$utoken = $token['access_token'];
$usecret = $token['refresh_token'];
echo $o->client_id;
echo $uid;
if ( is_array($token) && !isset($token['error']) ) {
mysql_connect('localhost', 'root', '');
mysql_select_db('freeimage');
$query = mysql_query("SELECT * FROM iusers WHERE oauth_provider = 'weibo' AND oauth_uid = ". $uid);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO iusers (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('weibo', {$uid}, '{$uname}', '{$utoken}', '{$usecret}')");
$query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
} else {
//Update the tokens
$query = mysql_query("UPDATE iusers SET oauth_token = '{$utoken}', oauth_secret = '{$usecret}' WHERE oauth_provider = 'weibo' AND oauth_uid = {$uid}");
}
$_SESSION['is_ilogin'] = 1;
setcookie("iuserid", $uid, time()+24*60*60);
setcookie("iusername", $uname, time()+24*60*60);
setcookie("iusertoken", $utoken, time()+24*60*60);
setcookie("iusersecret", $usecret, time()+24*60*60);
header('Location: index.php');
} else {
header('Location: login.php');
}
?>
授权完成,<a href="weibolist.php">进入你的微博列表页面</a><br />
<?php
} else {
?>
授权失败。
<?php
}
?>