FishWh.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace clue;
  3. /**
  4. * 飞鱼线索处理
  5. */
  6. class FishWh
  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/fishWh/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 getAnalysisLiveList($token,$param)
  190. {
  191. $url = $this->domain . 'open_api/2/report/live_room/analysis/get/';
  192. $headers = array(
  193. 'Access-Token: ' . $token
  194. );
  195. $info = [
  196. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  197. 'start_time' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  198. 'end_time' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  199. 'page' => $param['page'],
  200. 'fields' =>$param['fields'],
  201. 'page_size' => 100,
  202. ];
  203. $args = [];
  204. foreach ($info as $key => $value) {
  205. $args[$key] = is_string($value) ? $value : json_encode($value);
  206. }
  207. $url = $url . "?" . http_build_query($args);
  208. $headers = array('Access-Token: ' . $token,);
  209. return $this->getCurl($url, $headers);
  210. }
  211. /**
  212. * 获取直播数据
  213. */
  214. public function getLiveAttribute($token,$param)
  215. {
  216. $url = $this->domain . 'open_api/2/report/live_room/attribute/get/';
  217. $headers = array(
  218. 'Access-Token: ' . $token
  219. );
  220. $info = [
  221. 'advertiser_id' => $param['advertiser_id'],//广告主ID
  222. 'start_time' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
  223. 'end_time' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
  224. 'page' => $param['page'],
  225. 'page_size' => 100,
  226. ];
  227. $args = [];
  228. foreach ($info as $key => $value) {
  229. $args[$key] = is_string($value) ? $value : json_encode($value);
  230. }
  231. $url = $url . "?" . http_build_query($args);
  232. $headers = array('Access-Token: ' . $token,);
  233. return $this->getCurl($url, $headers);
  234. }
  235. /**
  236. * curl请求下get数据
  237. */
  238. public function getCurl($url, $headers)
  239. {
  240. $ch=curl_init($url);
  241. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  242. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  243. curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
  244. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  245. $output=curl_exec($ch);
  246. curl_close($ch);
  247. return json_decode($output,true);
  248. }
  249. /**
  250. * curl请求
  251. */
  252. public function curl($url, $param, $header)
  253. {
  254. $curl = curl_init();
  255. curl_setopt($curl, CURLOPT_URL, $url);
  256. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  257. curl_setopt($curl, CURLOPT_POST, 1);
  258. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  259. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  260. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  261. curl_setopt($curl, CURLOPT_HEADER, false);
  262. $rs = curl_exec($curl);
  263. curl_close($curl);
  264. return json_decode($rs,true);
  265. }
  266. }