wxauthor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $_SESSION['wxauthor_time'] = time();
  53. return $user_info;
  54. }
  55. }
  56. private function access_token($code)
  57. {
  58. $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
  59. $res = http_request(self::access_token_url,$params);
  60. if($res == false) return false;
  61. /*
  62. * 正确返回
  63. * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
  64. * 错误返回
  65. * {"errcode":40029,"errmsg":"invalid code"}
  66. */
  67. $info = json_decode($res, true);
  68. $this->mErrcode = intval($info['errcode']);
  69. if($this->mErrcode > 0) {
  70. $this->mErrmsg = $info['errmsg'];
  71. return false;
  72. }
  73. else {
  74. $this->mAccessToken = $info['access_token'];
  75. $this->mOpenID = $info['openid'];
  76. return true;
  77. }
  78. }
  79. private function user_info()
  80. {
  81. $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
  82. $res = http_request(self::userinfo_url,$params);
  83. if($res == false) return false;
  84. $info = json_decode($res, true);
  85. $this->mErrcode = intval($info['errcode']);
  86. if($this->mErrcode > 0) {
  87. $this->mErrmsg = $info['errmsg'];
  88. return false;
  89. }
  90. else {
  91. Log::record("user_info={$res}",Log::DEBUG);
  92. return $info;
  93. }
  94. }
  95. }