Oauth.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /* PHP SDK
  3. * @version 2.0.0
  4. * @author connect@qq.com
  5. * @copyright © 2013, Tencent Corporation. All rights reserved.
  6. */
  7. require_once(CLASS_PATH."Recorder.class.php");
  8. require_once(CLASS_PATH."URL.class.php");
  9. require_once(CLASS_PATH."ErrorCase.class.php");
  10. class Oauth{
  11. const VERSION = "2.0";
  12. const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
  13. const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
  14. const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
  15. protected $recorder;
  16. public $urlUtils;
  17. protected $error;
  18. function __construct(){
  19. $this->recorder = new Recorder();
  20. $this->urlUtils = new URL();
  21. $this->error = new ErrorCase();
  22. }
  23. public function qq_login(){
  24. $appid = $this->recorder->readInc("appid");
  25. $callback = $this->recorder->readInc("callback");
  26. $scope = $this->recorder->readInc("scope");
  27. //-------生成唯一随机串防CSRF攻击
  28. //$state = md5(uniqid(rand(), TRUE));
  29. $this->recorder->write('state',$state);
  30. //-------构造请求参数列表
  31. $keysArr = array(
  32. "response_type" => "code",
  33. "client_id" => $appid,
  34. "redirect_uri" => $callback,
  35. "state" => $state,
  36. "scope" => $scope
  37. );
  38. $login_url = $this->urlUtils->combineURL(self::GET_AUTH_CODE_URL, $keysArr);
  39. header("Location:$login_url");
  40. }
  41. public function qq_callback(){
  42. $state = $this->recorder->read("state");
  43. //--------验证state防止CSRF攻击
  44. if($_GET['state'] != $state){
  45. $this->error->showError("30001");
  46. }
  47. //-------请求参数列表
  48. $keysArr = array(
  49. "grant_type" => "authorization_code",
  50. "client_id" => $this->recorder->readInc("appid"),
  51. "redirect_uri" => urlencode($this->recorder->readInc("callback")),
  52. "client_secret" => $this->recorder->readInc("appkey"),
  53. "code" => $_GET['code']
  54. );
  55. //------构造请求access_token的url
  56. $token_url = $this->urlUtils->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr);
  57. $response = $this->urlUtils->get_contents($token_url);
  58. if(strpos($response, "callback") !== false){
  59. $lpos = strpos($response, "(");
  60. $rpos = strrpos($response, ")");
  61. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  62. $msg = json_decode($response);
  63. if(isset($msg->error)){
  64. $this->error->showError($msg->error, $msg->error_description);
  65. }
  66. }
  67. $params = array();
  68. parse_str($response, $params);
  69. $this->recorder->write("access_token", $params["access_token"]);
  70. return $params["access_token"];
  71. }
  72. public function get_openid(){
  73. //-------请求参数列表
  74. $keysArr = array(
  75. "access_token" => $this->recorder->read("access_token")
  76. );
  77. $graph_url = $this->urlUtils->combineURL(self::GET_OPENID_URL, $keysArr);
  78. $response = $this->urlUtils->get_contents($graph_url);
  79. //--------检测错误是否发生
  80. if(strpos($response, "callback") !== false){
  81. $lpos = strpos($response, "(");
  82. $rpos = strrpos($response, ")");
  83. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  84. }
  85. $user = json_decode($response);
  86. if(isset($user->error)){
  87. $this->error->showError($user->error, $user->error_description);
  88. }
  89. //------记录openid
  90. $this->recorder->write("openid", $user->openid);
  91. return $user->openid;
  92. }
  93. }