User.php 5.1 KB

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