User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 微信粉丝管理
  19. * Class User
  20. * @package WeChat
  21. */
  22. class User extends BasicWeChat
  23. {
  24. /**
  25. * 设置用户备注名
  26. * @param string $openid
  27. * @param string $remark
  28. * @return array
  29. * @throws Exceptions\InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function updateMark($openid, $remark)
  33. {
  34. $url = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN';
  35. $this->registerApi($url, __FUNCTION__, func_get_args());
  36. return $this->httpPostForJson($url, ['openid' => $openid, 'remark' => $remark]);
  37. }
  38. /**
  39. * 获取用户基本信息(包括UnionID机制)
  40. * @param string $openid
  41. * @param string $lang
  42. * @return array
  43. * @throws Exceptions\InvalidResponseException
  44. * @throws Exceptions\LocalCacheException
  45. */
  46. public function getUserInfo($openid, $lang = 'zh_CN')
  47. {
  48. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid={$openid}&lang={$lang}";
  49. $this->registerApi($url, __FUNCTION__, func_get_args());
  50. return $this->httpGetForJson($url);
  51. }
  52. /**
  53. * 批量获取用户基本信息
  54. * @param array $openids
  55. * @param string $lang
  56. * @return array
  57. * @throws Exceptions\InvalidResponseException
  58. * @throws Exceptions\LocalCacheException
  59. */
  60. public function getBatchUserInfo(array $openids, $lang = 'zh_CN')
  61. {
  62. $url = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN';
  63. $data = ['user_list' => []];
  64. foreach ($openids as $openid) {
  65. $data['user_list'][] = ['openid' => $openid, 'lang' => $lang];
  66. }
  67. $this->registerApi($url, __FUNCTION__, func_get_args());
  68. return $this->httpPostForJson($url, $data);
  69. }
  70. /**
  71. * 获取用户列表
  72. * @param string $next_openid
  73. * @return array
  74. * @throws Exceptions\InvalidResponseException
  75. * @throws Exceptions\LocalCacheException
  76. */
  77. public function getUserList($next_openid = '')
  78. {
  79. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid={$next_openid}";
  80. $this->registerApi($url, __FUNCTION__, func_get_args());
  81. return $this->httpGetForJson($url);
  82. }
  83. /**
  84. * 获取标签下粉丝列表
  85. * @param integer $tagid 标签ID
  86. * @param string $next_openid 第一个拉取的OPENID
  87. * @return array
  88. * @throws Exceptions\InvalidResponseException
  89. * @throws Exceptions\LocalCacheException
  90. */
  91. public function getUserListByTag($tagid, $next_openid = '')
  92. {
  93. $url = 'https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=ACCESS_TOKEN';
  94. $this->registerApi($url, __FUNCTION__, func_get_args());
  95. return $this->httpPostForJson($url, ['tagid' => $tagid, 'next_openid' => $next_openid]);
  96. }
  97. /**
  98. * 获取公众号的黑名单列表
  99. * @param string $begin_openid
  100. * @return array
  101. * @throws Exceptions\InvalidResponseException
  102. * @throws Exceptions\LocalCacheException
  103. */
  104. public function getBlackList($begin_openid = '')
  105. {
  106. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist?access_token=ACCESS_TOKEN";
  107. $this->registerApi($url, __FUNCTION__, func_get_args());
  108. return $this->httpPostForJson($url, ['begin_openid' => $begin_openid]);
  109. }
  110. /**
  111. * 批量拉黑用户
  112. * @param array $openids
  113. * @return array
  114. * @throws Exceptions\InvalidResponseException
  115. * @throws Exceptions\LocalCacheException
  116. */
  117. public function batchBlackList(array $openids)
  118. {
  119. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist?access_token=ACCESS_TOKEN";
  120. $this->registerApi($url, __FUNCTION__, func_get_args());
  121. return $this->httpPostForJson($url, ['openid_list' => $openids]);
  122. }
  123. /**
  124. * 批量取消拉黑用户
  125. * @param array $openids
  126. * @return array
  127. * @throws Exceptions\InvalidResponseException
  128. * @throws Exceptions\LocalCacheException
  129. */
  130. public function batchUnblackList(array $openids)
  131. {
  132. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist?access_token=ACCESS_TOKEN";
  133. $this->registerApi($url, __FUNCTION__, func_get_args());
  134. return $this->httpPostForJson($url, ['openid_list' => $openids]);
  135. }
  136. }