国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

81 行
3.4KB

  1. <?php
  2. namespace WeChat;
  3. if (!defined('DEDEINC')) exit('dedebiz');
  4. use WeChat\Contracts\BasicWeChat;
  5. /**
  6. * 微信网页授权
  7. * Class Oauth
  8. * @package WeChat
  9. */
  10. class Oauth extends BasicWeChat
  11. {
  12. /**
  13. * Oauth 授权跳转接口
  14. * @param string $redirect_url 授权回跳地址
  15. * @param string $state 为重定向后会带上state参数(填写a-zA-Z0-9的参数值,最多128字节)
  16. * @param string $scope 授权类类型(可选值snsapi_base|snsapi_userinfo)
  17. * @return string
  18. */
  19. public function getOauthRedirect($redirect_url, $state = '', $scope = 'snsapi_base')
  20. {
  21. $appid = $this->config->get('appid');
  22. $redirect_uri = urlencode($redirect_url);
  23. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
  24. }
  25. /**
  26. * 通过 code 获取 AccessToken 和 openid
  27. * @param string $code 授权Code值,不传则取GET参数
  28. * @return array
  29. * @throws \WeChat\Exceptions\InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function getOauthAccessToken($code = '')
  33. {
  34. $appid = $this->config->get('appid');
  35. $appsecret = $this->config->get('appsecret');
  36. $code = $code ? $code : (isset($_GET['code']) ? $_GET['code'] : '');
  37. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
  38. return $this->httpGetForJson($url);
  39. }
  40. /**
  41. * 刷新AccessToken并续期
  42. * @param string $refresh_token
  43. * @return array
  44. * @throws \WeChat\Exceptions\InvalidResponseException
  45. * @throws \WeChat\Exceptions\LocalCacheException
  46. */
  47. public function getOauthRefreshToken($refresh_token)
  48. {
  49. $appid = $this->config->get('appid');
  50. $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$appid}&grant_type=refresh_token&refresh_token={$refresh_token}";
  51. return $this->httpGetForJson($url);
  52. }
  53. /**
  54. * 检验授权凭证(access_token)是否有效
  55. * @param string $access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
  56. * @param string $openid 会员的唯一标识
  57. * @return array
  58. * @throws \WeChat\Exceptions\InvalidResponseException
  59. * @throws \WeChat\Exceptions\LocalCacheException
  60. */
  61. public function checkOauthAccessToken($access_token, $openid)
  62. {
  63. $url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$openid}";
  64. return $this->httpGetForJson($url);
  65. }
  66. /**
  67. * 拉取会员信息(需scope为 snsapi_userinfo)
  68. * @param string $access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
  69. * @param string $openid 会员的唯一标识
  70. * @param string $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
  71. * @return array
  72. * @throws \WeChat\Exceptions\InvalidResponseException
  73. * @throws \WeChat\Exceptions\LocalCacheException
  74. */
  75. public function getUserInfo($access_token, $openid, $lang = 'zh_CN')
  76. {
  77. $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang={$lang}";
  78. return $this->httpGetForJson($url);
  79. }
  80. }
  81. ?>