forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
105 lines (85 loc) · 2.52 KB
/
User.php
File metadata and controls
105 lines (85 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Thenbsp\Wechat\User;
use Thenbsp\Wechat\Bridge\Http;
use Thenbsp\Wechat\Wechat\AccessToken;
use Doctrine\Common\Collections\ArrayCollection;
class User
{
/**
* 获取用户信息
*/
const USERINFO = 'https://api.weixin.qq.com/cgi-bin/user/info';
/**
* 批量获取用户
*/
const BETCH = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget';
/**
* 获取用户列表
*/
const LISTS = 'https://api.weixin.qq.com/cgi-bin/user/get';
/**
* Thenbsp\Wechat\Wechat\AccessToken
*/
protected $accessToken;
/**
* 构造方法
*/
public function __construct(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}
/**
* 查询用户列表
*/
public function lists($nextOpenid = null)
{
$query = is_null($nextOpenid)
? array()
: array('next_openid'=>$nextOpenid);
$response = Http::request('GET', static::LISTS)
->withAccessToken($this->accessToken)
->withQuery($query)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return $response;
}
/**
* 获取用户信息
*/
public function get($openid, $lang = 'zh_CN')
{
$query = array(
'openid' => $openid,
'lang' => $lang
);
$response = Http::request('GET', static::USERINFO)
->withAccessToken($this->accessToken)
->withQuery($query)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return $response;
}
/**
* 批量获取用户信息
*/
public function getBetch(array $openid, $lang = 'zh_CN')
{
$body = array();
foreach($openid as $key=>$value) {
$body['user_list'][$key]['openid'] = $value;
$body['user_list'][$key]['lang'] = $lang;
}
$response = Http::request('POST', static::BETCH)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return new ArrayCollection($response['user_info_list']);
}
}