ToutiaoAdapter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\adapter;
  3. // 头条-适配器
  4. use GuzzleHttp\Client;
  5. class ToutiaoAdapter
  6. {
  7. public function OAuth($info)
  8. {
  9. // 执行验证callback, 返回授权后的字段,跟数据库对应
  10. return [
  11. "advertiser_id" => "",
  12. "advertiser_name" => "",
  13. "access_token" => "",
  14. "refresh_token" => "",
  15. "expire_time" => "",
  16. ];
  17. }
  18. // 获取子账号列表
  19. public function getSubAccounts($pmid)
  20. {
  21. }
  22. // 刷新 token
  23. public function refreshToken($pmid)
  24. {
  25. }
  26. // 获取小时消耗
  27. public function getHourlyCost($advertiserId, $accessToken, $date): array
  28. {
  29. $page = 1;
  30. $data = [];
  31. do {
  32. $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
  33. $request_data = [
  34. 'advertiser_id'=>$advertiserId,
  35. 'dimensions'=>json_encode(["cdp_project_id","cdp_project_name","ad_platform_cdp_project_action_track_url","ad_platform_cdp_project_download_url","stat_time_hour"]),
  36. 'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","convert_cnt"]),
  37. 'filters'=>'[{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
  38. 'start_time'=>$date,
  39. 'end_time'=>$date,
  40. 'order_by'=>json_encode([]),
  41. 'page'=>$page,
  42. 'page_size'=>100,
  43. ];
  44. $options = [
  45. "headers" => [
  46. 'Access-Token' => $accessToken,
  47. 'Content-Type' => 'application/json',
  48. ]
  49. ];
  50. $url = $url."?".http_build_query($request_data);
  51. $httpClient = new Client(['timeout' => 10]);
  52. $res = $httpClient->request('GET', $url, $options);
  53. $result = json_decode($res->getBody(), true);
  54. if (empty($result['data']['rows'])) {
  55. break;
  56. }
  57. $data = array_merge($data, $result['data']['rows']);
  58. $totalPage = $result['data']['page_info']['total_page'] ?? 1;
  59. $page++;
  60. } while ($page <= $totalPage);
  61. return $data;
  62. }
  63. // 获取媒体消耗
  64. public function getVideoCost($advertiserId, $accessToken, $date): array
  65. {
  66. $page = 1;
  67. $data = [];
  68. do {
  69. $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
  70. $request_data = [
  71. 'advertiser_id'=>$advertiserId,
  72. 'dimensions'=>json_encode(["ad_platform_material_name","material_id","cdp_project_id","cdp_project_name","stat_time_day", "ad_platform_cdp_project_action_track_url"]),
  73. 'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","active","active_register","active_pay","valid_play","download_finish_cnt","total_play", "stat_pay_amount"]),
  74. 'filters'=>'[{"field":"image_mode","type":1,"operator":0,"values":["5","15"]},{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
  75. 'start_time'=>$date,
  76. 'end_time'=>$date,
  77. 'order_by'=>json_encode([]),
  78. 'page'=>1,
  79. 'page_size'=>100,
  80. ];
  81. $options = [
  82. "headers" => [
  83. 'Access-Token' => $accessToken,
  84. 'Content-Type' => 'application/json',
  85. ]
  86. ];
  87. $url = $url."?".http_build_query($request_data);
  88. $httpClient = new Client(['timeout' => 10]);
  89. $res = $httpClient->request('GET', $url, $options);
  90. $result = json_decode($res->getBody(), true);
  91. if (empty($result['data']['rows'])) {
  92. break;
  93. }
  94. $data = array_merge($data, $result['data']['rows']);
  95. $totalPage = $result['data']['page_info']['total_page'] ?? 1;
  96. $page++;
  97. } while ($page <= $totalPage);
  98. return $data;
  99. }
  100. public function getVideo($advertiserId, $accessToken, $materialIds): array
  101. {
  102. $url = 'https://api.oceanengine.com/open_api/2/file/video/get/';
  103. $request_data = [
  104. 'advertiser_id'=>$advertiserId,
  105. 'filtering' => json_encode([
  106. "material_ids" => $materialIds,
  107. ]),
  108. ];
  109. $options = [
  110. "headers" => [
  111. 'Access-Token' => $accessToken,
  112. 'Content-Type' => 'application/json',
  113. ]
  114. ];
  115. $url = $url."?".http_build_query($request_data);
  116. $httpClient = new Client(['timeout' => 10]);
  117. $res = $httpClient->request('GET', $url, $options);
  118. $result = json_decode($res->getBody(), true);
  119. return $result['data']['list'] ?? [];
  120. }
  121. }