Fans.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Controllers\Wechat;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Ixudra\Curl\Facades\Curl;
  6. class Fans extends Controller
  7. {
  8. //
  9. public static function getTotal()
  10. {
  11. $openids = array();
  12. $next_openid = null;
  13. $i = 0;
  14. do {
  15. $http = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . Base::getAccessToken() . '&next_openid=' . $next_openid;
  16. $result = json_decode(Curl::to($http)->get(), true);
  17. $openids = array_merge($openids, $result['data']['openid']);
  18. $next_openid = $result['next_openid'];
  19. $i += 10000;
  20. } while ($i < $result['total']);
  21. return $openids;
  22. }
  23. public static function batchGetInfo(array $openids)
  24. {
  25. $array = [];
  26. foreach ($openids as $openid) {
  27. $array[] = [
  28. 'openid' => $openid
  29. ];
  30. }
  31. $data = array();
  32. for ($i = 0; $i < count($array); $i += 100) {
  33. $post = json_encode(['user_list' => array_slice($array, $i, 100)]);
  34. $http = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=' . Base::getAccessToken();
  35. $result = json_decode(Curl::to($http)->withData($post)->post(), true);
  36. $data = array_merge($data, $result['user_info_list']);
  37. }
  38. return $data;
  39. }
  40. public static function getUserInfo(string $openid)
  41. {
  42. $http = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . Base::getAccessToken() . '&openid=' . $openid . '&lang=zh_CN';
  43. $result = json_decode(Curl::to($http)->get(), true);
  44. if (isset($result['errcode'])) {
  45. return false;
  46. }
  47. return $result;
  48. }
  49. }