wxauthor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/25
  6. * Time: 下午10:50
  7. */
  8. namespace thrid_author;
  9. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  10. require_once(BASE_ROOT_PATH . '/core/framework/function/http.php');
  11. use util;
  12. use Log;
  13. class wxauthor
  14. {
  15. const appid = 'wx6b42e00ecaade538';
  16. const appsecret ='ee64233b3144d76217161666f8cb4c86';
  17. const authorize_url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
  18. const access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  19. const userinfo_url = 'https://api.weixin.qq.com/sns/userinfo';
  20. private $mAccessToken;
  21. private $mOpenID;
  22. private $mErrcode;
  23. private $mErrmsg;
  24. public function __construct()
  25. {
  26. $this->mErrcode = 0;
  27. }
  28. public function enter($origin_url)
  29. {
  30. $back_url = BASE_SITE_URL . '/mobile/wxauthor.php';
  31. $params = ['appid' => self::appid,
  32. 'redirect_uri' => $back_url,
  33. 'response_type' => 'code',
  34. 'scope' => 'snsapi_userinfo',
  35. 'state' => $_SESSION['MPHPSESSID']];
  36. $ref_url = util::http_add_params(self::authorize_url,$params);
  37. $ref_url .= '#wechat_redirect';
  38. $_SESSION['author_orgin_url'] = $origin_url;
  39. fcgi_header("location:{$ref_url}");
  40. }
  41. public function callback($code)
  42. {
  43. if($this->access_token($code) == false) {
  44. Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  45. return false;
  46. }
  47. $user_info = $this->user_info();
  48. if($user_info == false) {
  49. Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  50. return false;
  51. } else {
  52. return $user_info;
  53. }
  54. }
  55. private function access_token($code)
  56. {
  57. $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
  58. $res = http_request(self::access_token_url,$params);
  59. if($res == false) return false;
  60. /*
  61. * 正确返回
  62. * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
  63. * 错误返回
  64. * {"errcode":40029,"errmsg":"invalid code"}
  65. */
  66. $info = json_decode($res, true);
  67. $this->mErrcode = intval($info['errcode']);
  68. if($this->mErrcode > 0) {
  69. $this->mErrmsg = $info['errmsg'];
  70. return false;
  71. }
  72. else {
  73. $this->mAccessToken = $info['access_token'];
  74. $this->mOpenID = $info['openid'];
  75. return true;
  76. }
  77. }
  78. private function user_info()
  79. {
  80. $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
  81. $res = http_request(self::userinfo_url,$params);
  82. if($res == false) return false;
  83. $info = json_decode($res, true);
  84. $this->mErrcode = intval($info['errcode']);
  85. if($this->mErrcode > 0) {
  86. $this->mErrmsg = $info['errmsg'];
  87. return false;
  88. }
  89. else {
  90. Log::record("user_info={$info}",Log::DEBUG);
  91. return $info;
  92. }
  93. }
  94. }