qq登陆
2015-06-18 15:20
155 查看
<?php // =============qq登陆(仅供测试使用)============== $qq = new QQ; if( !isset($_GET['code']) ){ $qq->login(); }else{ $qq->access_token($_GET['code']); $qq->get_user_info(); // 获取用户信息。 } class QQ{ private $appid,$appkey,$redirect_uri,$access_token,$openid,$img; public function __construct(){ $this->appid = ""; $this->appkey = ""; $this->redirect_uri = "http://www.useryx.net"; $this->img = "./qq_login.png"; // qq登陆图片 echo '<html> <head> <meta charset="UTF-8"> <meta property="qc:admins" content="126626657765352106654" /> </head> <body>'; } // 测试 public function html($url){ // 等同于加载模板 echo '<a href="'.$url.'"><img src="'.$this->img.'" alt="QQ登陆"></a>'; } // qq登陆,页面 public function login(){ // $this->ceshi(); exit; // 此处打开做验证。默认关闭。 $url = "https://graph.qq.com/oauth2.0/authorize"; $array = array( "response_type" => "code", "client_id" => $this->appid, "redirect_uri" => $this->redirect_uri, "state" => time(), "scope" => "get_user_info,get_info,add_t,del_t,add_pic_t,get_repost_list,get_other_info,get_fanslist,get_idollist,add_idol,del_idol", // 以获取的权限。 ); $url = $url.'?'.http_build_query($array); $this->html($url); } public function access_token($code){ $url = "https://graph.qq.com/oauth2.0/token"; $array = array( "grant_type" => "authorization_code", "client_id" => $this->appid, "client_secret" => $this->appkey, "code" => $code, "redirect_uri" => $this->redirect_uri, ); $url = $url.'?'.http_build_query($array); $token = file_get_contents($url); // 获取token,解析字符串 $a = explode("&",$token); for ($i=0; $i < count($a); $i++) { $ar = explode("=",$a[$i]); $arr[$ar[0]] = $ar[1]; } $this->access_token = $arr['access_token']; $this->openid(); } // 用户基础信息。 public function openid(){ $url = "https://graph.qq.com/oauth2.0/me?access_token=$this->access_token"; $str = file_get_contents($url); // 正则匹配 preg_match("/{.*}/i",$str,$m); $user = json_decode($m[0],true); // echo '<pre>用户基础信息为:<br>'; // print_r($user); // echo '<pre>'; $this->openid = $user['openid']; } public function get_user_info(){ $url = "https://graph.qq.com/user/get_user_info?"; $array = array( "access_token" => $this->access_token, "oauth_consumer_key" => $this->appid, "openid" => $this->openid, ); $get_user_info = file_get_contents($url.http_build_query($array)); $get_user_info = json_decode($get_user_info,true); echo '<pre>'; print_r($get_user_info); } // 析构方法 public function __destruct(){ echo "</body> </html>"; } }
相关文章推荐
- 插件开发技术(9)---SQL查询记录与对象的映射
- 支付宝手机网站即时交易 自己封装的类
- linux环境下python 库模块安装
- Search Insert Position
- Objective-C----实例变量
- 1.13
- ISO7816 (part 1-3) asynchronous smartcard information
- servlet交互案例分析及终极实现 使用jdbc连接数据库实现
- centos安装配置lnmp
- ofbiz中的container-1
- Access数据库技巧汇集
- sublime设置缩进和禁止更新
- 关于GPRS模块启动问题。
- phonegap(cordova)在ios系统实现录音功能的几种特殊情况
- Complete list of APDU responses
- java 反射创建对象并传入参数
- 领域驱动设计之领域模型
- 【程序员面试宝典】找规律
- php socket
- Android Canvas绘图详解