token.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. include "config.php";
  4. include "mysql.lib.php";
  5. class Token
  6. {
  7. public $wechat_error;
  8. public $wechat_code;
  9. public $wechat_url;
  10. public $token;
  11. public $openid;
  12. public $access_info;
  13. public function __construct($error_code = false)
  14. {
  15. $this->wechat_error = $error_code;
  16. $this->GetWechatAccessToken();
  17. $this->SetOpenID();
  18. }
  19. /****
  20. * 获取 wechat code
  21. */
  22. public function GetWechatCode()
  23. {
  24. $url = WECHAT_SERVER . '?appid=' . WECHAT_APPID . '&redirect_uri=' . WECHAT_REDIRECT_URL . '&response_type=code&scope=' . WECHAT_SCOPE_BASE . '&state=1#wechat_redirect';
  25. $this->wechat_code = file_get_contents($url);
  26. $this->wechat_url = $url;
  27. return $this->wechat_code;
  28. }
  29. public function GetWechatAccessToken()
  30. {
  31. $this->GetWechatCode();
  32. $url = WECHAT_ACCESSTOKEN_URL . '?appid=' . WECHAT_APPID . '&secret=' . WECHAT_KEY . '&code=' . $this->wechat_code . '&grant_type=authorization_code';
  33. $this->access_info = file_get_contents($url);
  34. return $this->access_info;
  35. }
  36. public function SetOpenID($data)
  37. {
  38. $this->openid = json_decode($data, true)['openid'];
  39. }
  40. }
  41. /*
  42. $token = new Token();
  43. $cc = $token->GetWechatCode();
  44. error_log("code#######" . $cc, '3', 'D:\xampp\htdocs\wechat\log\error.txt');
  45. error_log("wechat_url#######" . $token->wechat_url, '3', 'D:\xampp\htdocs\wechat\log\error.txt');
  46. var_dump($token->wechat_code);*/