qq_callback.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once(PLUGINS_PATH .DIRECTORY_SEPARATOR. 'login'.DIRECTORY_SEPARATOR. 'qq'.DIRECTORY_SEPARATOR.'comm'.DIRECTORY_SEPARATOR."config.php");
  3. require_once(PLUGINS_PATH .DIRECTORY_SEPARATOR. 'login'.DIRECTORY_SEPARATOR. 'qq'.DIRECTORY_SEPARATOR.'comm'.DIRECTORY_SEPARATOR."utils.php");
  4. function qq_callback()
  5. {
  6. if(input('param.state') == session('state')) //csrf
  7. {
  8. $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
  9. . "client_id=" . session("appid"). "&redirect_uri=" . urlencode(session("callback"))
  10. . "&client_secret=" . session("appkey"). "&code=" . input("code");
  11. $response = get_url_contents($token_url);
  12. if (strpos($response, "callback") !== false)
  13. {
  14. $lpos = strpos($response, "(");
  15. $rpos = strrpos($response, ")");
  16. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  17. $msg = json_decode($response);
  18. if (isset($msg->error))
  19. {
  20. echo "<h3>error:</h3>" . $msg->error;
  21. echo "<h3>msg :</h3>" . $msg->error_description;
  22. }
  23. }
  24. $params = array();
  25. parse_str($response, $params);
  26. //set access token to session
  27. session("access_token",$params["access_token"]);
  28. }
  29. else
  30. {
  31. echo("The state does not match. You may be a victim of CSRF.");
  32. }
  33. }
  34. function get_openid()
  35. {
  36. $graph_url = "https://graph.qq.com/oauth2.0/me?access_token="
  37. . session('access_token');
  38. $str = get_url_contents($graph_url);
  39. if (strpos($str, "callback") !== false)
  40. {
  41. $lpos = strpos($str, "(");
  42. $rpos = strrpos($str, ")");
  43. $str = substr($str, $lpos + 1, $rpos - $lpos -1);
  44. }
  45. $user = json_decode($str);
  46. if (isset($user->error))
  47. {
  48. echo "<h3>error:</h3>" . $user->error;
  49. echo "<h3>msg :</h3>" . $user->error_description;
  50. }
  51. //set openid to session
  52. session('openid',$user->openid);
  53. }
  54. //QQ登录成功后的回调地址,主要保存access token
  55. qq_callback();
  56. //获取用户标示id
  57. get_openid();
  58. @header('location: '.HOME_SITE_URL.'/Connectqq');
  59. ?>