function Tencent() { require_once APP_ROOT_PATH . 'system/api_login/Tencent/Tencent.php'; OAuth::init($GLOBALS['m_config']['tencent_app_key'], $GLOBALS['m_config']['tencent_app_secret']); $openid = trim($GLOBALS['request']['openid']); $openkey = trim($GLOBALS['request']['openkey']); if ($GLOBALS['m_config']['tencent_bind_url'] == "") { $app_url = get_domain() . APP_ROOT . "/api_callback.php?c=Tencent"; } else { $app_url = $GLOBALS['m_config']['tencent_bind_url']; } $access_token = trim($GLOBALS['request']['access_token']); es_session::set("t_access_token", $access_token); es_session::set("t_openid", $openid); es_session::set("t_openkey", $openkey); if (es_session::get("t_access_token") || es_session::get("t_openid") && es_session::get("t_openkey")) { $r = Tencent::api('user/info'); $r = json_decode($r, true); $name = $r['data']['name']; if ($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "user where tencent_id = '" . $name . "'") == 0) { $GLOBALS['db']->query("update " . DB_PREFIX . "user set t_access_token ='" . $access_token . "',t_openkey = '" . $openkey . "',t_openid = '" . $openid . "', tencent_id = '" . $name . "' where id =" . intval($GLOBALS['user_info']['id'])); } elseif (intval($GLOBALS['db']->getOne("select id from " . DB_PREFIX . "user where tencent_id = '" . $name . "'")) == intval($GLOBALS['user_info']['id'])) { $GLOBALS['db']->query("update " . DB_PREFIX . "user set t_access_token ='" . $access_token . "',t_openkey = '" . $openkey . "',t_openid = '" . $openid . "', tencent_id = '" . $name . "' where id =" . intval($GLOBALS['user_info']['id'])); } else { $root['return'] = 0; $root['info'] = "该微博帐号已被其他会员绑定"; output($root); } } $root['return'] = 1; $root['info'] = "绑定成功"; $root['login_type'] = "Tencent"; output($root); }
/** * qqweibo用图片URL发表带图片的微博 */ public function addQQWeiboPic($bindinfo,$params){ $_SESSION['qqweibo']['t_access_token'] = $bindinfo['snsbind_accesstoken']; $_SESSION['qqweibo']['t_openid'] = $bindinfo['snsbind_openid']; include_once(BASE_DATA_PATH.DS.'api'.DS.'snsapi'.DS.'qqweibo'.DS.'tencent.php'); $params_qqweibo['content'] = $params['title'].$params['comment'].$params['url']; $params_qqweibo['pic_url'] = $params['images']; Tencent::api('t/add_pic_url', $params_qqweibo, 'POST'); }
/** * 验证授权是否有效 */ public static function checkOAuthValid() { $r = json_decode(Tencent::api('user/info'), true); if ($r['data']['name']) { return true; } else { self::clearOAuthInfo(); return false; } }
public function getuserinfo() { if ($_SESSION['t_access_token'] || $_SESSION['t_openid'] && $_SESSION['t_openkey']) { //用户已授权 //echo '<pre><h3>已授权</h3>用户信息:<br>'; //获取用户信息 $r = Tencent::api('user/info'); return json_decode($r, true); } }
function actionReturn() { if ($_GET['code']) { //已获得code $code = $_GET['code']; $openid = $_GET['openid']; $openkey = $_GET['openkey']; //获取授权token $url = \OAuth::getAccessToken($code, $this->url); $access_token = $_SESSION['t_access_token']; $r = \Http::request($url); parse_str($r, $out); //存储授权数据 if ($out['access_token']) { $_SESSION['t_access_token'] = $out['access_token']; $_SESSION['t_expire_in'] = $out['expire_in']; $_SESSION['t_code'] = $code; $_SESSION['t_openid'] = $openid; $_SESSION['t_openkey'] = $openkey; //验证授权 $ret = \OAuth::checkOAuthValid(); $ret = \Tencent::api('user/info'); $uid_get = json_decode($ret, true); try { $uid = $uid_get['data']['openid']; $me['id'] = $uid; $me['name'] = $uid_get['data']['name']; $me['email'] = $uid_get['data']['email']; $me['nickname'] = $uid_get['data']['nick']; $r = $this->member_get_third_set_user($me, $this->oauth_id, $access_token); flash('success', __('login success')); $this->redirect(return_url()); } catch (OAuthException $e) { flash('error', __('login error')); $this->redirect(return_url()); } } } exit; }
function Tencent() { es_session::start(); require_once APP_ROOT_PATH . 'system/api_login/Tencent/Tencent.php'; OAuth::init($GLOBALS['m_config']['tencent_app_key'], $GLOBALS['m_config']['tencent_app_secret']); $openid = trim($GLOBALS['request']['openid']); $openkey = trim($GLOBALS['request']['openkey']); if ($GLOBALS['m_config']['tencent_bind_url'] == "") { $app_url = get_domain() . APP_ROOT . "/api_callback.php?c=Tencent"; } else { $app_url = $GLOBALS['m_config']['tencent_bind_url']; } $access_token = trim($GLOBALS['request']['access_token']); es_session::set("t_access_token", $access_token); es_session::set("t_openid", $openid); es_session::set("t_openkey", $openkey); if (es_session::get("t_access_token") || es_session::get("t_openid") && es_session::get("t_openkey")) { $r = Tencent::api('user/info'); $r = json_decode($r, true); $name = $r['data']['name']; $user_data = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where tencent_id = '" . $name . "' and tencent_id <> ''"); if ($user_data) { if ($user_data['is_effect'] == 0 || $user_data['is_delete'] == 1) { $result['resulttype'] = 0; } else { $user_current_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where id = " . intval($user_data['group_id'])); $user_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where score <=" . intval($user_data['score']) . " order by score desc"); if ($user_current_group['score'] < $user_group['score']) { $user_data['group_id'] = intval($user_group['id']); } $GLOBALS['db']->query("update " . DB_PREFIX . "user set t_access_token ='" . $access_token . "',t_openkey = '" . $openkey . "',t_openid = '" . $openid . "', login_ip = '" . get_client_ip() . "',login_time= " . get_gmtime() . ",group_id=" . intval($user_data['group_id']) . " where id =" . $user_data['id']); $GLOBALS['db']->query("update " . DB_PREFIX . "deal_cart set user_id = " . intval($user_data['id']) . " where session_id = '" . es_session::id() . "'"); $result['user_pwd'] = $user_data['user_pwd']; $result['uid'] = $user_data['id']; $result['email'] = $user_data['email']; $result['user_avatar'] = get_abs_img_root(get_muser_avatar($user_data['id'], "big")); $name = $user_data['user_name']; $result['resulttype'] = 1; } } else { $result['email'] = ""; $result['user_pwd'] = md5(get_gmtime()); $result['user_name'] = $name; $result['t_access_token'] = $access_token; $result['t_openkey'] = $openkey; $result['t_openid'] = $openid; $result['tencent_id'] = $name; $result['uid'] = bind_add_user($result); if ($result['uid'] > 0) { $result['user_avatar'] = ''; $result['resulttype'] = 1; } else { $result['resulttype'] = -1; } } } $result['openid'] = $openid; $result['openkey'] = $openkey; $result['access_token'] = $access_token; $result['tencent_id'] = $name; $result['user_name'] = $name; $result['act'] = "synclogin"; $result['login_type'] = "Tencent"; output($result); }
public function send_message($data) { require_once APP_ROOT_PATH . 'system/api_login/Tencent/Tencent.php'; OAuth::init($this->api['config']['app_key'], $this->api['config']['app_secret']); $uid = intval($GLOBALS['user_info']['id']); $udata = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . $uid); es_session::set("t_access_token", $udata['t_access_token']); es_session::set("t_openid", $udata['t_openid']); es_session::set("t_openkey", $udata['t_openkey']); if (es_session::get("t_access_token") || es_session::get("t_openid") && es_session::get("t_openkey")) { if (!empty($data['img'])) { $params = array('content' => $data['content'], 'clientip' => get_client_ip(), 'format' => 'json'); $multi = array('pic' => $data['img']); $r = Tencent::api('t/add_pic', $params, 'POST', $multi); } else { $params = array('content' => $data['content'], 'clientip' => get_client_ip(), 'format' => 'json'); $r = Tencent::api('t/add', $params, 'POST'); } $msg = json_decode($r, true); if (intval($msg['errcode']) == 0) { $result['status'] = true; $result['msg'] = "success"; ajax_return($result); } else { $result['status'] = false; $result['msg'] = "腾讯微博" . $msg['msg']; ajax_return($result); } } }
public static function oauthRoute($type) { global $kekezu, $weibo_list; require S_ROOT . "include/oauth/config.php"; if ($type === 'sina') { require S_ROOT . "include/oauth/sina/saetv2.ex.class.php"; $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY); $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL); } if ($type === 'qq') { require S_ROOT . "include/oauth/qq/qqConnectAPI.php"; $qqConnectAPI = new QC(); $qqConnectAPI->qq_login(); die; } if ($type === 'ten') { require S_ROOT . "include/oauth/ten/Tencent.php"; OAuth::init(TEN_AKEY, TEN_SKEY); Tencent::$debug = false; $code_url = OAuth::getAuthorizeURL(TEN_CALLBACK_URL); } if ($type === 'renren') { require S_ROOT . "include/oauth/renren/rennclient/RennClient.php"; $rennClient = new RennClient(APP_KEY, APP_SECRET); $state = uniqid('renren_', true); $_SESSION['renren_state'] = $state; $code_url = $rennClient->getAuthorizeURL(CALLBACK_URL, 'code', $state); } if ($type === 'douban') { require S_ROOT . 'include/oauth/douban/douban.php'; $douBan = new doubanPHP(DB_APIKEY, DB_SECRET); $code_url = $douBan->login_url(DB_CALLBACK_URL, DB_SCOPE); } header("location:" . $code_url); }
<?php /** * 此为PHP-SDK 2.0 */ require_once BASE_DATA_PATH . DS . 'api' . DS . 'snsapi' . DS . 'qqweibo' . DS . 'tencent.php'; require_once BASE_DATA_PATH . DS . 'api' . DS . 'snsapi' . DS . 'qqweibo' . DS . 'config.php'; OAuth::init($client_id, $client_secret); Tencent::$debug = $debug; if ($_GET['code']) { //已获得code $code = $_GET['code']; $openid = $_GET['openid']; $openkey = $_GET['openkey']; //获取授权token $url = OAuth::getAccessToken($code, $callback); $r = Http::request($url); parse_str($r, $out); //存储授权数据 if ($out['access_token']) { $_SESSION['qqweibo']['t_access_token'] = $out['access_token']; $_SESSION['qqweibo']['t_expire_in'] = $out['expires_in']; $_SESSION['qqweibo']['t_refresh_token'] = $out['refresh_token']; $_SESSION['qqweibo']['t_uname'] = $out['name']; $_SESSION['qqweibo']['t_code'] = $code; $_SESSION['qqweibo']['t_openid'] = $openid; //OpenID可以唯一标识一个用户。在同一个应用下,同一个QQ号码的OpenID是相同的;但在不同应用下,同一个QQ号码可能有不同的OpenID $_SESSION['qqweibo']['t_openkey'] = $openkey; //OpenKey是与OpenID对应的用户key(用户在第三方应用的腾讯登录态),是验证OpenID身份的验证密钥,大多数API的访问,都需要同时具备OpenID和OpenKey的信息,其有效期为2小时 //验证授权 $r = OAuth::checkOAuthValid();
if ($_SESSION['t_access_token'] || $_SESSION['t_openid'] && $_SESSION['t_openkey']) { //用户已授权 // 获取昵称 $info = Tencent::api('user/info', array(), 'GET'); $info = json_decode($info); $info = $info->data; $nick = $info->nick ? $info->nick : $info->name; $select = $_REQUEST['select']; $url = "http://digi2012.sinaapp.com/weibo.php?select={$select}&username={$nick}"; /** * 发表图片微博 * 如果图片地址为网络上的一个可用链接 * 则使用add_pic_url接口 * */ $params = array('content' => '2012我的消费电子心愿单出炉啦!快来秀秀你的选择吧,分享就有机会赢#腾讯数码扑克牌#实体版哦!http://digi.tech.qq.com/zt2012/poker/index.htm', 'pic_url' => $url); $r = Tencent::api('t/add_pic_url', $params, 'POST'); header('Location: http://k.t.qq.com/k/%25E8%2585%25BE%25E8%25AE%25AF%25E6%2595%25B0%25E7%25A0%2581%25E6%2589%2591%25E5%2585%258B%25E7%2589%258C'); } else { //未授权 $callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?select=' . $_REQUEST['select']; //回调url if ($_GET['code']) { //已获得code $code = $_GET['code']; $openid = $_GET['openid']; $openkey = $_GET['openkey']; //获取授权token $url = OAuth::getAccessToken($code, $callback); $r = Http::request($url); parse_str($r, $out); //存储授权数据
public function upload($text, $opt, $pic) { if (file_exists($pic)) { $params = array('content' => $text); $multi = array('pic' => $pic); return Tencent::api('t/add_pic', $params, 'POST', $multi); } else { return $this->update($text, $opt); } }
public function callback() { OAuth::init($this->api['config']['app_key'], $this->api['config']['app_secret']); Tencent::$debug = $debug; $callback = SITE_DOMAIN . APP_ROOT . "/api_callback.php?c=Tencent"; if (es_session::is_set('t_access_token') || es_session::is_set('t_openid') && es_session::is_set('t_openkey')) { //用户已授权 //echo '<pre><h3>已授权</h3>用户信息:<br>'; //获取用户信息 $r = Tencent::api('user/info'); $json_data = json_decode($r, true); //print_r($json_data); // echo '</pre>'; } else { if ($_GET['code']) { //已获得code $code = $_GET['code']; $openid = $_GET['openid']; $openkey = $_GET['openkey']; //获取授权token $url = OAuth::getAccessToken($code, $callback); $r = Http::request($url); parse_str($r, $out); //存储授权数据 if ($out['access_token']) { es_session::set('t_access_token', $out['access_token']); es_session::set('refresh_token', $out['refresh_token']); es_session::set('expires_in', $out['expires_in']); es_session::set('t_code', $code); es_session::set('t_openid', $openid); es_session::set('t_openkey', $openkey); //验证授权 $r = OAuth::checkOAuthValid(); if ($r) { app_redirect($callback); //刷新页面 } else { exit('<h3>授权失败,请重试</h3>'); } } else { exit($r); } } else { //获取授权code if ($_GET['openid'] && $_GET['openkey']) { //应用频道 s_session::set('t_openid', $_GET['openid']); es_session::set('t_openkey', $_GET['openkey']); //验证授权 $r = OAuth::checkOAuthValid(); if ($r) { app_redirect($callback); //刷新页面 } else { exit('<h3>授权失败,请重试</h3>'); } } else { $url = OAuth::getAuthorizeURL($callback); app_redirect($url); } } } if ($json_data['msg'] != "ok") { echo '<pre><h3>出错了</h3><pre>'; die; } $is_bind = intval($_REQUEST['is_bind']); $tencent_id = $json_data['data']['openid']; $msg['field'] = 'tencent_id'; $msg['id'] = $tencent_id; $msg['name'] = $json_data['data']['name']; es_session::set("api_user_info", $msg); $user_data = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where tencent_id = '" . $tencent_id . "' and tencent_id <> ''"); if ($user_data) { $user_current_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where id = " . intval($user_data['group_id'])); $user_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where score <=" . intval($user_data['score']) . " order by score desc"); if ($user_current_group['score'] < $user_group['score']) { $user_data['group_id'] = intval($user_group['id']); } //$GLOBALS['db']->query("update ".DB_PREFIX."user set tencent_app_key ='".$last_key['oauth_token']."',tencent_app_secret = '".$last_key['oauth_token_secret']."', login_ip = '".get_client_ip()."',login_time= ".TIME_UTC.",group_id=".intval($user_data['group_id'])." where id =".$user_data['id']); //$GLOBALS['db']->query("update ".DB_PREFIX."deal_cart set user_id = ".intval($user_data['id'])." where session_id = '".es_session::id()."'"); es_session::delete("api_user_info"); if ($is_bind) { if (intval($user_data['id']) != intval($GLOBALS['user_info']['id'])) { showErr("该帐号已经被别的会员绑定过,请直接用帐号登录", 0, url("shop", "uc_center#setweibo")); } else { es_session::set("user_info", $user_data); app_redirect(url("shop", "uc_center#setweibo")); } } else { es_session::set("user_info", $user_data); app_recirect_preview(); } } elseif ($is_bind == 1 && $GLOBALS['user_info']) { //当有用户身份且要求绑定时 $GLOBALS['db']->query("update " . DB_PREFIX . "user set tencent_id= '" . $tencent_id . "' where id =" . $GLOBALS['user_info']['id']); app_redirect(url("index", "uc_center#setweibo")); } else { $this->create_user(); //app_redirect(url("index","user#api_login")); app_recirect_preview(); } }
* 此为PHP-SDK 2.0 的一个使用Demo,用于流程和接口调用演示 * 请根据自身需求和环境进相应的安全和兼容处理,勿直接用于生产环境 */ error_reporting(0); require_once './Config.php'; require_once './Tencent.php'; OAuth::init($client_id, $client_secret); Tencent::$debug = $debug; //打开session session_start(); header('Content-Type: text/html; charset=utf-8'); if ($_SESSION['t_access_token'] || $_SESSION['t_openid'] && $_SESSION['t_openkey']) { //用户已授权 echo '<pre><h3>已授权</h3>用户信息:<br>'; //获取用户信息 $r = Tencent::api('user/info'); print_r(json_decode($r, true)); echo '</pre>'; // 部分接口的调用示例 /** * 发表图片微博 * pic参数后跟图片的路径,以表单形式上传的为 : $_FILES['pic']['tmp_name'] * 服务器目录下的文件为: dirname(__FILE__).'/logo.png' * / $params = array( 'content' => '测试发表一条图片微博' ); $multi = array('pic' => dirname(__FILE__).'/logo.png'); $r = Tencent::api('t/add_pic', $params, 'POST', $multi); echo $r;
/** * 发表带图片的微博 qq. * * @param object $sdk OpenApiV3 Object * @param string $openid openid * @param string $openkey openkey * @param string $pf 平台 * * @return array 微博接口调用结果 */ public function add_weibo_pic($status = '新内容', $pic = '', $qq_list) { $_SESSION['t_access_token'] = $qq_list['SynchroUser']['oauth_token']; $_SESSION['t_openid'] = $qq_list['SynchroUser']['account']; $t_client_id = $this->UserApp->find('first', array('conditions' => array('UserApp.type' => 'QQWeibo'))); $_SESSION['t_client_id'] = $t_client_id['UserApp']['app_key']; $sdk = new Tencent(); $params = array('content' => $status); if ($pic != '') { $multi = array('pic' => WWW_ROOT . $pic); $r = $sdk->api('t/add_pic', $params, 'POST', $multi); } else { $r = $sdk->api('t/add', $params, 'POST'); } $_r = json_decode($r, true); if (!empty($_r) && isset($_r['data']['id']) && $_r['msg'] == 'ok') { //分享记录 $this->data['OauthLog']['user_id'] = $_SESSION['User']['User']['id']; $this->data['OauthLog']['oauth_type'] = 'qq'; $this->data['OauthLog']['content'] = $status; $this->data['OauthLog']['modified'] = date('Y-m-d H:i:s'); $this->OauthLog->saveAll(array('OauthLog' => $this->data['OauthLog'])); } return $r; }
/** * 发表带图片的微博 qq. * * @param object $sdk OpenApiV3 Object * @param string $openid openid * @param string $openkey openkey * @param string $pf 平台 * * @return array 微博接口调用结果 */ public function add_weibo_pic($status = '新内容', $pic = '', $qq_list) { $_SESSION['t_access_token'] = $qq_list['SynchroUser']['oauth_token']; $_SESSION['t_openid'] = $qq_list['SynchroUser']['account']; $t_client_id = $this->UserApp->find('first', array('conditions' => array('UserApp.type' => 'QQWeibo'))); $_SESSION['t_client_id'] = $t_client_id['UserApp']['app_key']; $sdk = new Tencent(); $params = array('content' => $status); if ($pic != '') { $multi = array('pic' => WWW_ROOT . $pic); $r = $sdk->api('t/add_pic', $params, 'POST', $multi); } else { $r = $sdk->api('t/add', $params, 'POST'); } return $r; }