function get_info() { if ($this->dx_auth->is_logged_in()) { echo "你已经登陆了"; } else { $this->load->model('m_open'); session_start(); require_once APPPATH . 'libraries/weibo/config.php'; require_once APPPATH . 'libraries/weibo/saetv2.ex.class.php'; $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY); $o->set_debug(DEBUG_MODE); if (isset($_REQUEST['code'])) { $keys = array(); // 验证state $state = $_REQUEST['state']; if (empty($state) || $state !== $_SESSION['weibo_state']) { echo '非法请求!'; exit; } unset($_SESSION['weibo_state']); $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)); //echo "success"; $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']); $c->set_debug(DEBUG_MODE); $uid_get = $c->get_uid(); $uid = $uid_get['uid']; if (!$uid) { echo "error"; } $user = $c->show_user_by_id($uid); //根据ID获取用户等基本信息 //echo $user['screen_name']; if ($this->m_open->is_id($uid)) { $this->m_open->login($uid); //echo "登陆"; //echo $uid; //$c->update( "坑爹的api终于调好了"); redirect('line'); } else { $this->m_open->register($user); $this->m_open->create($user); //echo $uid; //echo "注册"; redirect('line'); } } else { echo "fail"; } } }
function open() { session_start(); require_once APPPATH . 'libraries/weibo/config.php'; require_once APPPATH . 'libraries/weibo/saetv2.ex.class.php'; $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY); $o->set_debug(DEBUG_MODE); if (isset($_REQUEST['code'])) { $keys = array(); // 验证state $state = $_REQUEST['state']; if (empty($state) || $state !== $_SESSION['weibo_state']) { echo '非法请求!'; exit; } unset($_SESSION['weibo_state']); $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)); echo "success"; $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']); $c->set_debug(DEBUG_MODE); $uid_get = $c->get_uid(); $uid = $uid_get['uid']; $user = $c->show_user_by_id($uid); //根据ID获取用户等基本信息 echo $user['screen_name']; //利用一个方法将weibo_id存入users //利用一个方法将weibo个人信息存入info中 } else { echo "fail"; } }
<?php session_start(); include_once 'config.php'; include_once 'saetv2.ex.class.php'; $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY); $o->set_debug(DEBUG_MODE); // 生成state并存入SESSION,以供CALLBACK时验证使用 $state = uniqid('weibo_', true); $_SESSION['weibo_state'] = $state; $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL, 'code', $state); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>新浪微博PHP SDK V2版 Demo - Powered by Sina App Engine</title> </head> <body> <p><a href="<?php echo $code_url; ?> "><img src="weibo_login.png" title="点击进入授权页面" alt="点击进入授权页面" border="0" /></a></p> <hr /> <p>新浪微博PHP SDK由新浪SAE团队开发和维护,已集成在新浪SAE平台,SAE团队会负责对其进行维护和更新,平台开发者无需自行下载更新即可直接调用最新SDK,使用微博最新API。</p> <p>本DEMO演示了PHP SDK的授权及接口调用方法,开发者可以在此基础上进行灵活多样的应用开发。</p> <hr /> <p>什么是新浪SAE?</p> <p>新浪SAE,全称Sina App Engine( <a href="http://sae.sina.com.cn" target="_blank">http://sae.sina.com.cn</a> ),是新浪研发中心推出的国内首个公有云计算平台。</p> <p>SAE选择在国内流行最广的Web开发语言PHP作为首选的支持语言, 同时还提供JAVA与Python语言的支持。</p>