1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @license MIT |
4
|
|
|
* @author zhangv |
5
|
|
|
*/ |
6
|
|
|
namespace zhangv\wechat; |
7
|
|
|
|
8
|
|
|
class WechatOAuth { |
9
|
|
|
const TICKETTYPE_JSAPI = 'jsapi',TICKETTYPE_WXCARD = 'wx_card'; |
10
|
|
|
public $responseJSON = null; |
11
|
|
|
public $errCode = null; |
12
|
|
|
public $errMsg = null; |
13
|
|
|
|
14
|
|
|
private $appId = null; |
15
|
|
|
private $appSecret = null; |
16
|
|
|
private $httpClient = null; |
17
|
|
|
private $accessToken = null; |
18
|
|
|
|
19
|
|
|
public function __construct($appId,$appSecret) { |
20
|
|
|
$this->appId = $appId; |
21
|
|
|
$this->appSecret = $appSecret; |
22
|
|
|
$this->httpClient = new HttpClient(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setHttpClient($httpClient){ |
26
|
|
|
$this->httpClient = $httpClient; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function authorizeURI($redirectURI,$scope = 'snsapi_userinfo',$state = ''){ |
30
|
|
|
$redirectURI = urlencode($redirectURI); |
31
|
|
|
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectURI}&response_type=code&scope=$scope&state=$state#wechat_redirect"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function authorize($code){ |
35
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code=$code&grant_type=authorization_code"; |
36
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
37
|
|
|
return json_decode($this->responseJSON); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getUserInfo($openId){ |
41
|
|
|
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->accessToken}&openid=$openId&lang=zh_CN"; |
42
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
43
|
|
|
return json_decode($this->responseJSON); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function refreshToken($refreshToken){ |
47
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appId}&grant_type=refresh_token&refresh_token=$refreshToken"; |
48
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
49
|
|
|
return json_decode($this->responseJSON); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function verifyToken($accessToken,$openId){ |
53
|
|
|
$url = "https://api.weixin.qq.com/sns/auth?access_token=$accessToken&openid=$openId"; |
54
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
55
|
|
|
return json_decode($this->responseJSON); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getAccessToken(){ |
59
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; |
60
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
61
|
|
|
$json = json_decode($this->responseJSON); |
62
|
|
|
$this->accessToken = $json->access_token; |
63
|
|
|
return $this->accessToken; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getTicket($type = WechatOAuth::TICKETTYPE_JSAPI, $accessToken = null){ |
67
|
|
|
if(!$accessToken) $accessToken = $this->getAccessToken(); |
68
|
|
|
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; |
69
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type={$type}&access_token=$accessToken"; |
70
|
|
|
$this->responseJSON = $this->httpClient->get($url); |
71
|
|
|
return json_decode($this->responseJSON); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|