Oauth.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const GET_USER_INFO_URL = "https://graph.qq.com/user/get_user_info";
  16. protected $recorder;
  17. public $urlUtils;
  18. protected $error;
  19. function __construct(){
  20. $this->recorder = new Recorder();
  21. $this->urlUtils = new URL();
  22. $this->error = new ErrorCase();
  23. }
  24. public function qq_login($callback = null,$appid = null){
  25. if(!$appid) {
  26. $appid = $this->recorder->readInc("appid");
  27. }
  28. if(!$callback) {
  29. $callback = $this->recorder->readInc("callback");
  30. }
  31. $scope = $this->recorder->readInc("scope");
  32. //-------生成唯一随机串防CSRF攻击
  33. $state = md5(uniqid(rand(), TRUE));
  34. $this->recorder->write('state',$state);
  35. //-------构造请求参数列表
  36. $keysArr = array(
  37. "response_type" => "code",
  38. "client_id" => $appid,
  39. "redirect_uri" => urlencode($callback),
  40. "state" => $state,
  41. "scope" => $scope
  42. );
  43. $login_url = $this->urlUtils->combineURL(self::GET_AUTH_CODE_URL, $keysArr);
  44. header("Location:$login_url");
  45. }
  46. public function qq_callback($callback = null,$appkey=null,$appid=null){
  47. $state = $this->recorder->read("state");
  48. //--------验证state防止CSRF攻击
  49. if($_GET['state'] != $state){
  50. $this->error->showError("30001");
  51. }
  52. if(!$appkey){
  53. $appkey= $this->recorder->readInc("appkey");
  54. }
  55. if(!$callback) {
  56. $callback = $this->recorder->readInc("callback");
  57. }
  58. if(!$appid){
  59. $appid=$this->recorder->readInc("appid");
  60. }
  61. //-------请求参数列表
  62. $keysArr = array(
  63. "grant_type" => "authorization_code",
  64. "client_id" => $appid,
  65. "redirect_uri" => urlencode($callback),
  66. "client_secret" => $appkey,
  67. "code" => $_GET['code']
  68. );
  69. //------构造请求access_token的url
  70. $token_url = $this->urlUtils->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr);
  71. $response = $this->urlUtils->get_contents($token_url);
  72. if(strpos($response, "callback") !== false){
  73. $lpos = strpos($response, "(");
  74. $rpos = strrpos($response, ")");
  75. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  76. $msg = json_decode($response);
  77. if(isset($msg->error)){
  78. $this->error->showError($msg->error, $msg->error_description);
  79. }
  80. }
  81. $params = array();
  82. parse_str($response, $params);
  83. $this->recorder->write("access_token", $params["access_token"]);
  84. return $params["access_token"];
  85. }
  86. public function get_openid(){
  87. //-------请求参数列表
  88. $keysArr = array(
  89. "access_token" => $this->recorder->read("access_token")
  90. );
  91. $graph_url = $this->urlUtils->combineURL(self::GET_OPENID_URL, $keysArr);
  92. $response = $this->urlUtils->get_contents($graph_url);
  93. //--------检测错误是否发生
  94. if(strpos($response, "callback") !== false){
  95. $lpos = strpos($response, "(");
  96. $rpos = strrpos($response, ")");
  97. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  98. }
  99. $user = json_decode($response);
  100. if(isset($user->error)){
  101. $this->error->showError($user->error, $user->error_description);
  102. }
  103. //------记录openid
  104. $this->recorder->write("openid", $user->openid);
  105. return $user->openid;
  106. }
  107. public function get_pc_user_info($access_token, $openid)
  108. {
  109. $appid = $this->recorder->readInc("appid");
  110. $use_info_keysArr = array(
  111. "access_token" => $access_token,
  112. "openid" => $openid,
  113. "oauth_consumer_key" => $appid,
  114. "format"=> "json",
  115. );
  116. $graph_use_info_url = $this->urlUtils->combineURL(self::GET_USER_INFO_URL, $use_info_keysArr);
  117. $response = $this->urlUtils->get_contents($graph_use_info_url);
  118. return json_decode($response, true);
  119. }
  120. }