auth.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * 微信oAuth认证示例
  4. */
  5. include("../wechat.class.php");
  6. class wxauth {
  7. private $options;
  8. public $open_id;
  9. public $wxuser;
  10. public function __construct($options){
  11. $this->options = $options;
  12. $this->wxoauth();
  13. session_start();
  14. }
  15. public function wxoauth(){
  16. $scope = 'snsapi_base';
  17. $code = isset($_GET['code'])?$_GET['code']:'';
  18. $token_time = isset($_SESSION['token_time'])?$_SESSION['token_time']:0;
  19. if(!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time>time()-3600)
  20. {
  21. if (!$this->wxuser) {
  22. $this->wxuser = $_SESSION['wxuser'];
  23. }
  24. $this->open_id = $_SESSION['open_id'];
  25. return $this->open_id;
  26. }
  27. else
  28. {
  29. $options = array(
  30. 'token'=>$this->options["token"], //填写你设定的key
  31. 'appid'=>$this->options["appid"], //填写高级调用功能的app id
  32. 'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
  33. );
  34. $we_obj = new Wechat($options);
  35. if ($code) {
  36. $json = $we_obj->getOauthAccessToken();
  37. if (!$json) {
  38. unset($_SESSION['wx_redirect']);
  39. die('获取用户授权失败,请重新确认');
  40. }
  41. $_SESSION['open_id'] = $this->open_id = $json["openid"];
  42. $access_token = $json['access_token'];
  43. $_SESSION['user_token'] = $access_token;
  44. $_SESSION['token_time'] = time();
  45. $userinfo = $we_obj->getUserInfo($this->open_id);
  46. if ($userinfo && !empty($userinfo['nickname'])) {
  47. $this->wxuser = array(
  48. 'open_id'=>$this->open_id,
  49. 'nickname'=>$userinfo['nickname'],
  50. 'sex'=>intval($userinfo['sex']),
  51. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  52. 'avatar'=>$userinfo['headimgurl']
  53. );
  54. } elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
  55. $userinfo = $we_obj->getOauthUserinfo($access_token,$this->open_id);
  56. if ($userinfo && !empty($userinfo['nickname'])) {
  57. $this->wxuser = array(
  58. 'open_id'=>$this->open_id,
  59. 'nickname'=>$userinfo['nickname'],
  60. 'sex'=>intval($userinfo['sex']),
  61. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  62. 'avatar'=>$userinfo['headimgurl']
  63. );
  64. } else {
  65. return $this->open_id;
  66. }
  67. }
  68. if ($this->wxuser) {
  69. $_SESSION['wxuser'] = $this->wxuser;
  70. $_SESSION['open_id'] = $json["openid"];
  71. unset($_SESSION['wx_redirect']);
  72. return $this->open_id;
  73. }
  74. $scope = 'snsapi_userinfo';
  75. }
  76. if ($scope=='snsapi_base') {
  77. $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  78. $_SESSION['wx_redirect'] = $url;
  79. } else {
  80. $url = $_SESSION['wx_redirect'];
  81. }
  82. if (!$url) {
  83. unset($_SESSION['wx_redirect']);
  84. die('获取用户授权失败');
  85. }
  86. $oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$scope);
  87. header('Location: ' . $oauth_url);
  88. }
  89. }
  90. }
  91. $options = array(
  92. 'token'=>'tokenaccesskey', //填写你设定的key
  93. 'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
  94. 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  95. );
  96. $auth = new wxauth($options);
  97. var_dump($auth->wxuser);