企微短剧业务系统

AdvertiserOpenApi.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Support\tencentAdSDK;
  3. use App\Log;
  4. use App\Models\TencentAdAuth;
  5. use App\Service\HttpService;
  6. class AdvertiserOpenApi extends BaseOpenApi
  7. {
  8. /**
  9. * 日报获取
  10. */
  11. public static function advertiserSpecificationGet($accountId, $retry = 5)
  12. {
  13. $interface = 'wechat_advertiser_specification/get';
  14. $url = 'https://api.e.qq.com/v1.3/' . $interface;
  15. $access_token = TencentAdAuth::getAccessToken($accountId);
  16. if (empty($access_token)) return false;
  17. $common_parameters = [
  18. 'access_token' => $access_token,
  19. 'timestamp' => time(),
  20. 'nonce' => md5(uniqid('', true))
  21. ];
  22. $parameters = [
  23. 'account_id' => $accountId,
  24. 'fields' => [
  25. 'wechat_account_id',
  26. 'wechat_account_name',
  27. 'corporation_name'
  28. ],
  29. 'page' => 1,
  30. 'page_size' => 10,
  31. ];
  32. $parameters = array_merge($common_parameters, $parameters);
  33. foreach ($parameters as $key => $value) {
  34. if (!is_string($value)) {
  35. $parameters[$key] = json_encode($value);
  36. }
  37. }
  38. $request_url = $url . '?' . http_build_query($parameters);
  39. do {
  40. $res = HttpService::httpGet($request_url);
  41. // 记录日志
  42. Log::logInfo('获取公众号详情信息API:' . $interface, [
  43. 'url' => $request_url,
  44. 'params' => $parameters,
  45. 'response' => $res,
  46. ], 'AdvertiserOpenApi');
  47. $resArr = json_decode($res, true);
  48. if (!$resArr) {
  49. $resArr = [
  50. 'code' => 666666,
  51. 'message' => '请求失败(自定义)'
  52. ];
  53. }
  54. } while(($resArr['code'] != 0) && (--$retry > 0));
  55. if ($resArr['code'] == 0) {
  56. return self::returnSuccess($resArr['data']);
  57. } else {
  58. return self::returnFail([], $resArr['message']);
  59. }
  60. }
  61. public static function getOrganizationAccount($accountId, $cursor, $retry = 0) {
  62. $interface = 'organization_account_relation/get';
  63. $url = 'https://api.e.qq.com/v3.0/' . $interface;
  64. $accessToken = TencentAdAuth::getAccessToken($accountId);
  65. if (empty($accessToken)) {
  66. Log::logError('获取accessToken失败', ['account_id' => $accountId], 'getOrganizationAccount');
  67. return false;
  68. }
  69. $parameters = array (
  70. 'access_token' => $accessToken,
  71. 'timestamp' => time(),
  72. 'nonce' => md5(uniqid('', true)),
  73. 'pagination_mode' => 'PAGINATION_MODE_CURSOR',
  74. "cursor" => $cursor,
  75. "page_size" => 100
  76. );
  77. foreach ($parameters as $key => $value) {
  78. if (!is_string($value)) {
  79. $parameters[$key] = json_encode($value);
  80. }
  81. }
  82. $request_url = $url . '?' . http_build_query($parameters);
  83. $response = HttpService::httpGet($request_url);
  84. if(empty($response) && $retry < 5) {
  85. sleep(1);
  86. $retry++;
  87. return self::getOrganizationAccount($accountId, $cursor, $retry);
  88. }
  89. // 记录日志
  90. Log::logInfo('获取业务单元下的投放账户:' . $interface, [
  91. 'url' => $request_url,
  92. 'params' => $parameters,
  93. 'response' => $response,
  94. ], 'getOrganizationAccount');
  95. return json_decode($response, true);
  96. }
  97. }