Fish.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace clue;
  3. /**
  4. * 飞鱼线索处理
  5. */
  6. class Fish
  7. {
  8. /**
  9. * 开发者应用ID及密钥
  10. */
  11. protected $appid;
  12. protected $secret;
  13. /**
  14. * 接口地址
  15. */
  16. protected $domain = 'https://ad.oceanengine.com/';
  17. /**
  18. * 授权回调地址
  19. */
  20. protected $cakkback = 'https://wzh.nczyzs.com/api/fish/fish_call_back/';
  21. public function __construct($appid,$secret)
  22. {
  23. $this->appid = $appid;
  24. $this->secret = $secret;
  25. }
  26. /**
  27. * 生成授权地址
  28. */
  29. public function authorizedAddress($root_id)
  30. {
  31. return $this->domain . "openapi/audit/oauth.html?app_id=" . $this->appid . "&material_auth=1&state=" . $root_id . "&redirect_uri=" . urlencode($this->cakkback);
  32. }
  33. /**
  34. * 获取token和refresh_token
  35. */
  36. public function getAccessToken($code)
  37. {
  38. $url = $this->domain . 'open_api/oauth2/access_token/';
  39. $param = [
  40. 'app_id' => intval($this->appid),
  41. 'secret' => $this->secret,
  42. 'grant_type' => 'auth_code',
  43. 'auth_code' => $code,
  44. ];
  45. return $this->curl($url, $param, ['Content-Type:' => 'application/json']);
  46. }
  47. /**
  48. * 获取线索列表
  49. */
  50. public function getClueListData($token, $advertiser_ids, $start_time, $end_time, $page, $page_size = 10)
  51. {
  52. //时间格式yyyy-MM-dd 或者 yyyy-MM-dd hh:mm:ss
  53. $value = explode(',',$advertiser_ids);
  54. $url = $this->domain . 'open_api/2/tools/clue/get/?advertiser_ids=' . json_encode($value) . '&start_time=' . urlencode($start_time) . '&end_time=' . urlencode($end_time) . '&page=' . $page . '&page_size=' . $page_size;
  55. $headers = array(
  56. 'Access-Token: ' . $token,
  57. 'X-Debug-Mode: 1'
  58. );
  59. return $this->getCurl($url, $headers);
  60. }
  61. /**
  62. * 获取已授权账户根据Token
  63. */
  64. public function getAdvertiser($token)
  65. {
  66. $url = $this->domain . 'open_api/2/user/info/';
  67. $headers = array(
  68. 'Access-Token: ' . $token
  69. );
  70. return $this->getCurl($url, $headers);
  71. }
  72. /**
  73. * 刷新token
  74. */
  75. public function getRefreshToken($token)
  76. {
  77. $url = $this->domain . 'open_api/oauth2/refresh_token/';
  78. $param = [
  79. 'app_id' => intval($this->appid),
  80. 'secret' => $this->secret,
  81. 'grant_type ' => 'refresh_token',
  82. 'refresh_token' => $token,
  83. ];
  84. return $this->curl($url, $param, ['Content-Type:' => 'application/json']);
  85. }
  86. /**
  87. * 获取线索外呼中间号(AXB呼叫能力)
  88. */
  89. public function getCallVirtualNumber($token,$info)
  90. {
  91. $url = $this->domain . 'open_api/2/tools/clue/call_virtual_number/get/';
  92. $args = [];
  93. foreach ($info as $key => $value) {
  94. $args[$key] = is_string($value) ? $value : json_encode($value);
  95. }
  96. $url = $url . "?" . http_build_query($args);
  97. $headers = array('Access-Token: ' . $token,);
  98. return $this->getCurl($url, $headers);
  99. }
  100. /**
  101. * 发起语音双呼(双呼呼叫能力)
  102. */
  103. public function createBridgeCall($token,$info)
  104. {
  105. $url = $this->domain . 'open_api/2/tools/clue/bridge_call/create/';
  106. $headers = array('Access-Token: ' . $token,'Content-Type: application/json');
  107. return $this->curl($url,json_encode($info), $headers);
  108. }
  109. /**
  110. * 获取广告主数据
  111. */
  112. public function getAdvertiserList($token,$param)
  113. {
  114. $url = $this->domain . 'open_api/2/report/advertiser/get/';
  115. $headers = array(
  116. 'Access-Token: ' . $token
  117. );
  118. $info = [
  119. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  120. 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  121. 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  122. 'page' => $param['page'],
  123. 'page_size' => $param['page_size'],
  124. ];
  125. $args = [];
  126. foreach ($info as $key => $value) {
  127. $args[$key] = is_string($value) ? $value : json_encode($value);
  128. }
  129. $url = $url . "?" . http_build_query($args);
  130. $headers = array('Access-Token: ' . $token,);
  131. return $this->getCurl($url, $headers);
  132. }
  133. /**
  134. * 获取广告组下广告计划数据
  135. */
  136. public function getAdList($token,$param)
  137. {
  138. $url = $this->domain . 'open_api/2/report/ad/get/';
  139. $headers = array(
  140. 'Access-Token: ' . $token
  141. );
  142. $info = [
  143. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  144. 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  145. 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  146. 'filtering' => [
  147. 'campaign_ids' => [$param['campaign_ids']],//广告组id列表:按照campaign_id过滤,最多支持100个
  148. ],
  149. 'group_by'=>['STAT_GROUP_BY_FIELD_ID'],
  150. 'page' => $param['page'],
  151. 'page_size' => 100,
  152. ];
  153. $args = [];
  154. foreach ($info as $key => $value) {
  155. $args[$key] = is_string($value) ? $value : json_encode($value);
  156. }
  157. $url = $url . "?" . http_build_query($args);
  158. $headers = array('Access-Token: ' . $token,);
  159. return $this->getCurl($url, $headers);
  160. }
  161. /**
  162. * 获取广告组数据-每天获取一次上一天的数据保存到数据库
  163. */
  164. public function getCampaignList($token,$param)
  165. {
  166. $url = $this->domain . 'open_api/2/report/campaign/get/';
  167. $headers = array(
  168. 'Access-Token: ' . $token
  169. );
  170. $info = [
  171. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  172. 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  173. 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  174. 'page' => $param['page'],
  175. 'group_by'=>['STAT_GROUP_BY_FIELD_ID'],
  176. 'page_size' => 100,
  177. ];
  178. $args = [];
  179. foreach ($info as $key => $value) {
  180. $args[$key] = is_string($value) ? $value : json_encode($value);
  181. }
  182. $url = $url . "?" . http_build_query($args);
  183. $headers = array('Access-Token: ' . $token,);
  184. return $this->getCurl($url, $headers);
  185. }
  186. /**
  187. * 获取自定义报表可用指标和维度
  188. */
  189. public function getReportCustomConfig($token,$param)
  190. {
  191. $url = $this->domain . 'open_api/v3.0/report/custom/config/get/';
  192. $headers = array(
  193. 'Access-Token: ' . $token
  194. );
  195. $info = [
  196. 'advertiser_id' => intval($param['advertiser_id']),
  197. 'data_topics' => ['BASIC_DATA']
  198. ];
  199. $args = [];
  200. foreach ($info as $key => $value) {
  201. $args[$key] = is_string($value) ? $value : json_encode($value);
  202. }
  203. $url = $url . "?" . http_build_query($args);
  204. $headers = array('Access-Token: ' . $token,);
  205. return $this->getCurl($url, $headers);
  206. }
  207. /**
  208. * 升级版获取广告组数据
  209. */
  210. public function getCampaignListV2($token,$param)
  211. {
  212. $url = $this->domain . 'open_api/v3.0/report/custom/get/';
  213. $headers = array(
  214. 'Access-Token: ' . $token
  215. );
  216. $info = [
  217. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  218. 'data_topic' => 'BASIC_DATA',
  219. 'dimensions' => ['stat_time_day','cdp_project_id','cdp_project_name'],
  220. 'start_time' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  221. 'end_time' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  222. 'metrics' => ['stat_cost','show_cnt','cpm_platform','click_cnt','cpc_platform','ctr','convert_cnt','conversion_cost','conversion_rate','deep_convert_cnt','deep_convert_cost','deep_convert_rate'],
  223. 'filters' => [],
  224. 'order_by' => [],
  225. 'page' => $param['page'],
  226. 'page_size' => 100,
  227. ];
  228. $args = [];
  229. foreach ($info as $key => $value) {
  230. $args[$key] = is_string($value) ? $value : json_encode($value);
  231. }
  232. $url = $url . "?" . http_build_query($args);
  233. $headers = array('Access-Token: ' . $token,);
  234. return $this->getCurl($url, $headers);
  235. }
  236. /**
  237. * 获取广告组下广告计划数据
  238. */
  239. public function getAdListV2($token,$param)
  240. {
  241. $url = $this->domain . 'open_api/v3.0/report/custom/get/';
  242. $headers = array(
  243. 'Access-Token: ' . $token
  244. );
  245. $info = [
  246. 'advertiser_id' => $param['advertiser_id'], //广告主ID
  247. 'data_topic' => 'BASIC_DATA',
  248. 'dimensions' => ['stat_time_day', 'cdp_promotion_id', 'cdp_promotion_name'],
  249. 'start_time' => $param['start_date'], //起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  250. 'end_time' => $param['end_date'], //结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  251. 'metrics' => ['stat_cost', 'show_cnt', 'cpm_platform', 'click_cnt', 'cpc_platform', 'ctr', 'convert_cnt', 'conversion_cost', 'conversion_rate'],
  252. 'filters' => [
  253. [
  254. 'field' => 'cdp_project_id',
  255. 'type' => 1,
  256. 'operator' => 1,
  257. 'values' => [$param['campaign_ids']]
  258. ]
  259. ],
  260. 'order_by' => [
  261. [
  262. 'field' => 'stat_cost',
  263. 'type' => 'ASC'
  264. ]
  265. ],
  266. 'page' => $param['page'],
  267. 'page_size' => 100,
  268. ];
  269. $args = [];
  270. foreach ($info as $key => $value) {
  271. $args[$key] = is_string($value) ? $value : json_encode($value);
  272. }
  273. $url = $url . "?" . http_build_query($args);
  274. $headers = array('Access-Token: ' . $token,);
  275. return $this->getCurl($url, $headers);
  276. }
  277. /**
  278. * 获取线索外呼录音文件下载链接
  279. */
  280. public function getClueRecordUrl($token,$info)
  281. {
  282. $url = $this->domain . 'open_api/2/tools/clue/contact_log/record_url/get/';
  283. $args = [];
  284. foreach ($info as $key => $value) {
  285. $args[$key] = is_string($value) ? $value : json_encode($value);
  286. }
  287. $url = $url . "?" . http_build_query($args);
  288. $headers = array('Access-Token: ' . $token,);
  289. return $this->getCurl($url, $headers);
  290. }
  291. /**
  292. * curl请求下get数据
  293. */
  294. public function getCurl($url, $headers)
  295. {
  296. $ch=curl_init($url);
  297. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  298. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  299. curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
  300. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  301. $output=curl_exec($ch);
  302. curl_close($ch);
  303. return json_decode($output,true);
  304. }
  305. /**
  306. * curl请求
  307. */
  308. public function curl($url, $param, $header)
  309. {
  310. $curl = curl_init();
  311. curl_setopt($curl, CURLOPT_URL, $url);
  312. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  313. curl_setopt($curl, CURLOPT_POST, 1);
  314. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  315. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  316. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  317. curl_setopt($curl, CURLOPT_HEADER, false);
  318. $rs = curl_exec($curl);
  319. curl_close($curl);
  320. return json_decode($rs,true);
  321. }
  322. }