/new88li/openid/qq/callback.php
PHP | 77 lines | 56 code | 11 blank | 10 comment | 8 complexity | 3c1aea1055c5ad7dab76b35b1999f870 MD5 | raw file
- <?php
- require_once("client.php");
- //var_dump($_SESSION);exit;
- $targetUrl = !empty($_COOKIE['S']['OPENID_REFFERURL'])?$_COOKIE['S']['OPENID_REFFERURL']:$system->base_url();
- if($_REQUEST['state'] == $_SESSION['state']){
- $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
- . "client_id=" . APP_KEY. "&redirect_uri=" . urlencode(CALLBACK)
- . "&client_secret=" . APP_SECRET. "&code=" . $_REQUEST["code"];
- $response = file_get_contents($token_url);
- if (strpos($response, "callback") !== false){
- $lpos = strpos($response, "(");
- $rpos = strrpos($response, ")");
- $response = substr($response, $lpos + 1, $rpos - $lpos -1);
- $msg = json_decode($response);
- if (isset($msg->error))
- {
- echo "<h3>error:</h3>" . $msg->error;
- echo "<h3>msg :</h3>" . $msg->error_description;
- exit;
- }
- }
- $params = array();
- parse_str($response, $params);
- //$_SESSION["access_token"] = $params["access_token"];
-
-
- //get_openid
- $graph_url = "https://graph.qq.com/oauth2.0/me?access_token="
- . $params["access_token"];
- $str = file_get_contents($graph_url);
- if (strpos($str, "callback") !== false)
- {
- $lpos = strpos($str, "(");
- $rpos = strrpos($str, ")");
- $str = substr($str, $lpos + 1, $rpos - $lpos -1);
- }
- $user = json_decode($str);
- //var_dump($user->openid);exit('get_openid()');
- if (isset($user->error))
- {
- echo "<h3>error:</h3>" . $user->error;
- echo "<h3>msg :</h3>" . $user->error_description;
- exit;
- }
-
- //echo("Hello " . $user->openid);
- $info = get_user_info($user->openid,$params["access_token"]);
- /*var_dump($info);exit;
- array(7) { ["ret"]=> int(0) ["msg"]=> string(0) "" ["nickname"]=> string(18) "??????" ["figureurl"]=> string(73) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/30" ["figureurl_1"]=> string(73) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/50" ["figureurl_2"]=> string(74) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/100" ["gender"]=> string(3) "?" }
- */
- $row = array(
- 'refer_id' => $user->openid,
- 'open_id' => $info['nickname'],
- //'email' => $info['qmail'],
- 'token' => serialize($params["access_token"]),
- 'open_type' => 'qq',
- );
- //var_dump($row);exit;
- $account = $system->loadModel('member/account');
- $user = $account->createotherlogin($row);
-
- if($user['member_refer'] != 'local'){
- $targetUrl = $system->mkUrl('passport','bind',array(base64_encode($targetUrl)));;
- }
- }else{
- echo("The state does not match. You may be a victim of CSRF.");
- }
- echo "<script>window.location=decodeURIComponent('".$targetUrl."');</script>";
- exit;
- ?>