Skip to content

Commit a759072

Browse files
committed
[更新]批量获取用户基本信息
1 parent b807208 commit a759072

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

Wechat/Lib/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Common {
2929
public $postxml;
3030
public $_msg;
3131
public $errCode = 0;
32-
public $errMsg = "no access";
32+
public $errMsg = "";
3333
public $config = array();
3434
private $_retry = FALSE;
3535

Wechat/Lib/Tools.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,23 @@ static public function httpGet($url) {
139139
* @param array|string $postdata
140140
* @return bool|mixed
141141
*/
142-
static public function httpPost($url, $postdata) {
142+
static public function httpPost($url, $data) {
143143
$ch = curl_init();
144144
curl_setopt($ch, CURLOPT_URL, $url);
145145
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
146146
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
147147
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
148148
curl_setopt($ch, CURLOPT_HEADER, FALSE);
149149
curl_setopt($ch, CURLOPT_POST, TRUE);
150-
if (is_array($postdata)) {
151-
foreach ($postdata as &$value) {
150+
if (is_array($data)) {
151+
foreach ($data as &$value) {
152152
if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', FALSE)) {
153153
$value = new CURLFile(realpath(trim($value, '@')));
154154
}
155155
}
156+
$data = http_build_query($data);
156157
}
157-
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
158+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
158159
$data = curl_exec($ch);
159160
curl_close($ch);
160161
if ($data) {

Wechat/WechatUser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WechatUser extends Common {
1818
/* 获取粉丝信息 */
1919
const USER_INFO_URL = '/user/info?';
2020
/* 批量获取粉丝信息 */
21-
const USER_BATCH_INFO_URL = '/user/info/batchget';
21+
const USER_BATCH_INFO_URL = '/user/info/batchget?';
2222
/* 更新粉丝标注 */
2323
const USER_UPDATEREMARK_URL = '/user/info/updateremark?';
2424

@@ -112,15 +112,15 @@ public function getUserInfo($openid) {
112112
* @param string $lang 指定返回语言
113113
* @return bool|mixed
114114
*/
115-
public function getUserBatchInfo(array $openids, $lang = 'zh-CN') {
115+
public function getUserBatchInfo(array $openids, $lang = 'zh_CN') {
116116
if (!$this->access_token && !$this->getAccessToken()) {
117117
return false;
118118
}
119-
$data = array();
120-
foreach ($openids as $openid) {
121-
$data[] = array('openid' => $openid, 'lang' => $lang);
119+
$data = array('user_list' => array());
120+
foreach (array_unique($openids) as $openid) {
121+
$data['user_list'][] = array('openid' => $openid, 'lang' => $lang);
122122
}
123-
$result = Tools::httpPost(self::API_URL_PREFIX . self::USER_BATCH_INFO_URL . "access_token={$this->access_token}", $data);
123+
$result = Tools::httpPost(self::API_URL_PREFIX . self::USER_BATCH_INFO_URL . "access_token={$this->access_token}", Tools::json_encode($data));
124124
if ($result) {
125125
$json = json_decode($result, true);
126126
if (isset($json['errcode']) && !isset($json['user_info_list'])) {

0 commit comments

Comments
 (0)