申请 API接口请进入 http://dev.t.qq.com/ 下载
请把控制器放在xxx.php/Index/index.html
或者隐藏Xxx.php 否则腾讯不支持 回调地址 xxx.php?id=xxx 或 xxx.php/Index/index.html
登陆成功回调地址 xxx.php?xxx=xxx 接收参数
下面是个人实例代码,喜欢自动收听转发微博的请收藏
案例地址 http://blog.flash127.com/
class LoginAction extends Action {
public function index(){
import('ORG.Util.Tencent'); //截入腾讯微博类
$client_id = '80xxxxx225'; //app_id
$client_secret = 'e65XXXXXXXXXXXX75c8b6'; //app_key
$debug = false; //调试模式
OAuth::init($client_id, $client_secret);
Tencent::$debug = $debug;
if ($_SESSION['t_access_token'] || ($_SESSION['t_openid'] && $_SESSION['t_openkey'])) {//用户已授权
//获取用户信息
$r = Tencent::api('user/info');
$aaa = json_decode($r, true);
//print_r($aaa);
$nick = $aaa['data']['nick'];
$name = $aaa['data']['name'];
$head = $aaa['data']['head'];
//收听用户
$add_t_name = array(
"format" => "xml",
'name' => 'xz52099888',
'fopenids' => 'add'
);
Tencent::api('friends/add',$add_t_name,"post");
//收听用户
$add_t_name127 = array(
"format" => "xml",
'name' => 'flash127',
'fopenids' => 'add'
);
Tencent::api('friends/add',$add_t_name127,"post");
//发表图片微博
$add_txt = array(
'format' => "json",
'content' => '我刚在魔法日志网制作了一个魔法日志,这家网站太强大了,全站都免费用,上百款都可以做,可以说是全网最多最牛B的,给你们地址: http://blog.flash127.com/',
'pic_url' => 'http://mf.52qzone.net/templets/mm/img/logo.png',
'syncflag'=> 0
);
Tencent::api('t/add_pic_url', $add_txt, 'POST');
$db=M('User');
$where['t']=$name;
$F=$db->where($where)->find(); //如果本地有此用户,直接登陆成功,否则写入数据库
if($F){
$_SESSION['id'] = $F['id'];
$_SESSION['t'] = $F['t'];
$_SESSION['head'] = $F['head'];
$data['login_time'] = time();
$data['login_ip'] = $ip;
$data['nick'] = $nick;
$data['head'] = $head;
$data['a'] = $F['a']+1;
$db->where($where)->save($data);
$this->success("登陆成功","/vip/");
}else{
$ip = get_client_ip();
$data['t'] = $name;
$data['nick'] = $nick;
$data['head'] = $head;
$data['reg_time'] = time();
$data['reg_ip'] = $ip;
$data['login_time'] = time();
$data['login_ip'] = $ip;
$is=$db->add($data);
if($is){
$_SESSION['id'] = $is;
$_SESSION['t'] = $name;
$_SESSION['head'] = $head;
$this->success("注册成功","/vip/");
}else{
$this->error("注册失败");
}
}
}else{
$callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];//回调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);
//存储授权数据
if ($out['access_token']) {
$_SESSION['t_access_token'] = $out['access_token'];
$_SESSION['t_refresh_token'] = $out['refresh_token'];
$_SESSION['t_expire_in'] = $out['expires_in'];
$_SESSION['t_code'] = $code;
$_SESSION['t_openid'] = $openid;
$_SESSION['t_openkey'] = $openkey;
//验证授权
$r = OAuth::checkOAuthValid();
if ($r) {
header('Location: ' . $callback);//刷新页面
} else {
exit('<h3>授权失败,请重试</h3>');
}
} else {
exit($r);
}
} else {//获取授权code
if ($_GET['openid'] && $_GET['openkey']){//应用频道
$_SESSION['t_openid'] = $_GET['openid'];
$_SESSION['t_openkey'] = $_GET['openkey'];
//验证授权
$r = OAuth::checkOAuthValid();
if ($r) {
header('Location: ' . $callback);//刷新页面
} else {
exit('<h3>授权失败,请重试</h3>');
}
} else{
$url = OAuth::getAuthorizeURL($callback);
header('Location: ' . $url);
}
}
}
}
}
新闻热点
疑难解答