1 <?php
2 namespace Api\Member;
3 class QQConnect{
4 /**
5 * 获取QQconnect Login 跳转到的地址值
6 * @return array 返回包含code state
7 *
8 **/
9 public function login($app_id, $callback, $scope){
10 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
11 $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="
12 .$app_id. "&redirect_uri=" . urlencode($callback)
13 . "&state=" . $_SESSION['state']
14 . "&scope=".urlencode($scope);
15 //显示出登录地址
16 header('Location:'.$login_url);
17 }
18 /**
19 * 获取access_token值
20 * @return array 返回包含access_token,过期时间的数组
21 * */
22 private function get_token($app_id,$app_key,$code,$callback,$state){
23 if($state !== $_SESSION['state']){
24 return false;
25 exit();
26 }
27 $url = "https://graph.qq.com/oauth2.0/token";
28 $param = array(
29 "grant_type" => "authorization_code",
30 "client_id" => $app_id,
31 "client_secret" => $app_key,
32 "code" => $code,
33 "state" => $state,
34 "redirect_uri" => $callback
35 );
36 $response = $this->get_url($url, $param);
37 if($response == false) {
38 return false;
39 }
40 $params = array();
41 parse_str($response, $params);
42 return $params["access_token"];
43 }
44
45 /**
46 * 获取client_id 和 openid
47 * @param $access_token access_token验证码
48 * @return array 返回包含 openid的数组
49 * */
50 private function get_openid($access_token) {
51 $url = "https://graph.qq.com/oauth2.0/me";
52 $param = array(
53 "access_token" => $access_token
54 );
55 $response = $this->get_url($url, $param);
56 if($response == false) {
57 return false;
58 }
59 if (strpos($response, "callback") !== false) {
60 $lpos = strpos($response, "(");
61 $rpos = strrpos($response, ")");
62 $response = substr($response, $lpos + 1, $rpos - $lpos -1);
63 }
64 $user = json_decode($response);
65 if (isset($user->error) || $user->openid == "") {
66 return false;
67 }
68 return $user->openid;
69 }
70 /**
71 * 获取用户信息
72 * @param $client_id
73 * @param $access_token
74 * @param $openid
75 * @return array 用户的信息数组
76 * */
77 public function get_user_info($app_id,$token,$openid){
78 $url = 'https://graph.qq.com/user/get_user_info?oauth_consumer_key='.$app_id.'&access_token='.$token.'&openid='.$openid.'&format=json';
79 $str = $this->get_url($url);
80 if($str == false) {
81 return false;
82 }
83 $arr = json_decode($str,true);
84 return $arr;
85 }
86
87 /**
88 * 请求URL地址,返回callback得到返回字符串
89 * @param $url qq提供的api接口地址
90 * */
91
92 public function callback($app_id, $app_key, $callback) {
93 $code = $_GET['code'];
94 $state = $_GET['state'];
95 $token = $this->get_token($app_id,$app_key,$code,$callback,$state);
96 $openid = $this->get_openid($token);
97 if(!$token || !$openid) {
98 return false;
99 exit();
100 }
101 return array('openid' => $openid, 'token' => $token);
102 }
103
104
105 /*
106 * HTTP GET Request
107 */
108 private function get_url($url, $param = null) {
109 if($param != null) {
110 $query = http_build_query($param);
111 $url = $url . '?' . $query;
112 }
113 $ch = curl_init();
114 if(stripos($url, "https://") !== false){
115 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
116 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
117 }
118
119 curl_setopt($ch, CURLOPT_URL, $url);
120 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
121 $content = curl_exec($ch);
122 $status = curl_getinfo($ch);
123 curl_close($ch);
124 if(intval($status["http_code"]) == 200) {
125 return $content;
126 }else{
127 echo $status["http_code"];
128 return false;
129 }
130 }
131
132 /*
133 * HTTP POST Request
134 */
135 private function post_url($url, $params) {
136 $ch = curl_init();
137 if(stripos($url, "https://") !== false) {
138 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
139 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
140 }
141
142 curl_setopt($ch, CURLOPT_URL, $url);
143 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
144 curl_setopt($ch, CURLOPT_POST, true);
145 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
146 $content = curl_exec($ch);
147 $status = curl_getinfo($ch);
148 curl_close($ch);
149 if(intval($status["http_code"]) == 200) {
150 return $content;
151 } else {
152 return false;
153 }
154 }
155 }