首页 > php教程 > php手册 > 腾讯微博登陆API接口-实例代码

腾讯微博登陆API接口-实例代码

WBOY
发布: 2016-06-07 11:39:43
原创
1223 人浏览过

++++++++++++++++++++++++++++++++++++++++++++++++++
→ 欢迎吐槽 欢迎吐血 欢迎不尊重他要作品的鸟人来骂 来加我QQ839024615来骂
++++++++++++++++++++++++++++++++++++++++++++++++++

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

AD:真正免费,域名+虚机+企业邮箱=0元

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板