Fans.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Services\WeChat;
  3. class Fans extends Base
  4. {
  5. public function __construct($publicId)
  6. {
  7. parent::__construct($publicId);
  8. }
  9. /**
  10. * 获取用户基本信息
  11. * @param string $openid
  12. * @return mixed
  13. * @throws WeChatException
  14. */
  15. public function info($openid)
  16. {
  17. $access_token = $this->accessToken;
  18. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
  19. $response = json_decode(getUTF8Sting(\Curl::to($url)->get()), true);
  20. if (isset($response['errcode']) && $response['errcode'] != 0) {
  21. throw new WeChatException($response['errmsg'], $response['errcode']);
  22. }
  23. return $response;
  24. }
  25. /**
  26. * 获取全部用户信息
  27. * @param string $next_openid
  28. * @return mixed
  29. * @throws WeChatException
  30. */
  31. public function getAll($next_openid = '')
  32. {
  33. $access_token = $this->accessToken;
  34. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid={$next_openid}";
  35. $response = \Curl::to($url)->asJson(true)->get();
  36. if (isset($response['errcode']) && $response['errcode'] != 0) {
  37. throw new WeChatException($response['errmsg'], $response['errcode']);
  38. }
  39. return $response;
  40. }
  41. /**
  42. * 批量获取用户基本信息
  43. * @param string|string[] $openid_list
  44. * @return mixed
  45. * @throws WeChatException
  46. */
  47. public function batchGet($openid_list)
  48. {
  49. $user_list = [];
  50. foreach ($openid_list as $openid) {
  51. array_push($user_list, [
  52. 'openid' => $openid,
  53. 'lang' => 'zh_CN',
  54. ]);
  55. }
  56. $access_token = $this->accessToken;
  57. $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={$access_token}";
  58. $response = \Curl::to($url)->withData([
  59. 'user_list' => $user_list,
  60. ])->asJson(true)->post();
  61. if (isset($response['errcode']) && $response['errcode'] != 0) {
  62. throw new WeChatException($response['errmsg'], $response['errcode']);
  63. }
  64. $user_info_list = $response['user_info_list'];
  65. return $user_info_list;
  66. }
  67. /**
  68. * 设置用户备注名
  69. * @param string $openid
  70. * @param string $remark
  71. * @return bool
  72. * @throws WeChatException
  73. */
  74. public function updateRemark($openid, $remark)
  75. {
  76. $post = [
  77. 'openid' => $openid,
  78. 'remark' => $remark,
  79. ];
  80. $access_token = $this->accessToken;
  81. $url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token={$access_token}";
  82. $response = \Curl::to($url)->withData($post)->asJson(true)->post();
  83. if (isset($response['errcode']) && $response['errcode'] != 0) {
  84. throw new WeChatException($response['errmsg'], $response['errcode']);
  85. }
  86. return true;
  87. }
  88. /**
  89. * 获取标签下粉丝列表
  90. * @param int $tag_id
  91. * @param string $next_openid
  92. * @return mixed
  93. * @throws WeChatException
  94. */
  95. public function tagUser($tag_id, $next_openid = '')
  96. {
  97. $access_token = $this->accessToken;
  98. $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={$access_token}";
  99. $response = \Curl::to($url)->withData([
  100. 'tagid' => $tag_id,
  101. 'next_openid' => $next_openid,
  102. ])->asJson(true)->post();
  103. if (isset($response['errcode']) && $response['errcode'] != 0) {
  104. throw new WeChatException($response['errmsg'], $response['errcode']);
  105. }
  106. return $response;
  107. }
  108. }