auth.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return $this->open_id;
  25. }
  26. else
  27. {
  28. $options = array(
  29. 'token'=>$this->options["token"], //填写你设定的key
  30. 'appid'=>$this->options["appid"], //填写高级调用功能的app id
  31. 'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
  32. );
  33. $we_obj = new Wechat($options);
  34. if ($code) {
  35. $json = $we_obj->getOauthAccessToken();
  36. if (!$json) {
  37. unset($_SESSION['wx_redirect']);
  38. die('获取用户授权失败,请重新确认');
  39. }
  40. $_SESSION['open_id'] = $this->open_id = $json["openid"];
  41. $access_token = $json['access_token'];
  42. $_SESSION['user_token'] = $access_token;
  43. $_SESSION['token_time'] = time();
  44. $userinfo = $we_obj->getUserInfo($this->open_id);
  45. if ($userinfo && !empty($userinfo['nickname'])) {
  46. $this->wxuser = array(
  47. 'open_id'=>$this->open_id,
  48. 'nickname'=>$userinfo['nickname'],
  49. 'sex'=>intval($userinfo['sex']),
  50. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  51. 'avatar'=>$userinfo['headimgurl']
  52. );
  53. } elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
  54. $userinfo = $we_obj->getOauthUserinfo($access_token,$this->open_id);
  55. if ($userinfo && !empty($userinfo['nickname'])) {
  56. $this->wxuser = array(
  57. 'open_id'=>$this->open_id,
  58. 'nickname'=>$userinfo['nickname'],
  59. 'sex'=>intval($userinfo['sex']),
  60. 'location'=>$userinfo['province'].'-'.$userinfo['city'],
  61. 'avatar'=>$userinfo['headimgurl']
  62. );
  63. } else {
  64. return $this->open_id;
  65. }
  66. }
  67. if ($this->wxuser) {
  68. $_SESSION['wxuser'] = $this->wxuser;
  69. $_SESSION['open_id'] = $json["openid"];
  70. unset($_SESSION['wx_redirect']);
  71. return $this->open_id;
  72. }
  73. $scope = 'snsapi_userinfo';
  74. }
  75. if ($scope=='snsapi_base') {
  76. $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  77. $_SESSION['wx_redirect'] = $url;
  78. } else {
  79. $url = $_SESSION['wx_redirect'];
  80. }
  81. if (!$url) {
  82. unset($_SESSION['wx_redirect']);
  83. die('获取用户授权失败');
  84. }
  85. $oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$scope);
  86. header('Location: ' . $oauth_url);
  87. }
  88. }
  89. }
  90. $options = array(
  91. 'token'=>'tokenaccesskey', //填写你设定的key
  92. 'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
  93. 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  94. );
  95. $auth = new wxauth($options);
  96. var_dump($auth->wxuser);