qq_callback.php 2.3 KB

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