Tencent.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace clue;
  3. /**
  4. * 腾讯云线索
  5. */
  6. class Tencent
  7. {
  8. /**
  9. * 开发者应用ID及密钥
  10. */
  11. protected $appid;
  12. protected $secret;
  13. /**
  14. * 接口地址
  15. */
  16. protected $domain = 'https://api.e.qq.com/';
  17. /**
  18. * 授权回调地址
  19. */
  20. protected $cakkback = 'https://wzh.nczyzs.com/api/fish/tx_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 "https://developers.e.qq.com/oauth/authorize?client_id=" . $this->appid . "&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 . 'oauth/token';
  39. $param = [
  40. 'client_id' => intval($this->appid),
  41. 'client_secret' => $this->secret,
  42. 'grant_type' => 'authorization_code',
  43. 'authorization_code' => $code,
  44. 'redirect_uri' => $this->cakkback
  45. ];
  46. return $this->getCurl($url, $param, ['Content-Type:' => 'application/json']);
  47. }
  48. /**
  49. * 获取腾讯线索
  50. */
  51. public function getClueListData($token, $advertiser_ids, $start_time, $end_time, $page, $page_size)
  52. {
  53. $url = $this->domain . '/v1.3/lead_clues/get';
  54. $common_parameters = [
  55. 'access_token' => $token,
  56. 'timestamp' => time(),
  57. 'nonce' => str_rand(10),
  58. ];
  59. $parameters = [
  60. 'account_id' => intval($advertiser_ids), //单一获取(多个广告主需要循环)
  61. 'time_range' => [
  62. 'start_time' => $start_time, //时间戳
  63. 'end_time' => $end_time, //时间戳
  64. ],
  65. 'page' => $page,
  66. 'page_size' => $page_size,
  67. ];
  68. $request_url = $url . '?' . http_build_query($common_parameters);
  69. $output = $this->getTxCurl($request_url, $parameters);
  70. return json_decode($output, true);
  71. }
  72. /**
  73. * 获取广告组数据-每天获取一次上一天的数据保存到数据库
  74. */
  75. function getCampaignList($token,$ad,$time)
  76. {
  77. $url = $this->domain . '/v1.3/daily_reports/get';
  78. $common_parameters = array(
  79. 'access_token' => $token,
  80. 'timestamp' => time(),
  81. 'nonce' => str_rand(10),
  82. );
  83. $parameters = [
  84. 'account_id' => intval($ad),
  85. 'level' => 'REPORT_LEVEL_ADGROUP',
  86. 'date_range' => [
  87. 'start_date' => $time,
  88. 'end_date' => $time,
  89. ],
  90. 'group_by' => ['date','adgroup_id'],
  91. 'page' => 1,
  92. 'page_size' => 1000,
  93. 'fields' => ['campaign_id', 'account_id', 'id', 'date', 'view_count', 'valid_click_count', 'ctr', 'cpc', 'cost', 'adgroup_id', 'ad_id', 'view_count', 'adgroup_name', 'thousand_display_price', 'conversions_count', 'effective_cost']
  94. ];
  95. $parameters = array_merge($common_parameters, $parameters);
  96. return $this->getCurl($url, $parameters,['Content-Type:' => 'application/json']);
  97. }
  98. /**
  99. * 获取广告组下的详细
  100. */
  101. function getCampaignAdList($token,$ad,$time,$gid)
  102. {
  103. $url = $this->domain . '/v1.3/daily_reports/get';
  104. $common_parameters = array(
  105. 'access_token' => $token,
  106. 'timestamp' => time(),
  107. 'nonce' => str_rand(10),
  108. );
  109. $parameters = [
  110. 'account_id' => intval($ad),
  111. 'level' => 'REPORT_LEVEL_AD',
  112. 'date_range' => [
  113. 'start_date' => $time,
  114. 'end_date' => $time,
  115. ],
  116. "filtering" => [
  117. [
  118. "field" => 'adgroup_id',
  119. 'operator' => 'EQUALS',
  120. 'values' => [$gid]
  121. ]
  122. ],
  123. 'group_by' => ['date', 'ad_id'],
  124. 'page' => 1,
  125. 'page_size' => 1000,
  126. 'fields' => ['campaign_id', 'account_id', 'id', 'date', 'view_count', 'valid_click_count', 'ctr', 'cpc', 'cost', 'ad_name', 'deep_convert', 'adgroup_id', 'ad_id', 'view_count', 'adgroup_name', 'thousand_display_price', 'conversions_count', 'effective_cost']
  127. ];
  128. $parameters = array_merge($common_parameters, $parameters);
  129. return $this->getCurl($url, $parameters, ['Content-Type:' => 'application/json']);
  130. }
  131. /**
  132. * 获取中间号
  133. */
  134. public function getCallVirtualNumber($token, $tmp)
  135. {
  136. $url = $this->domain . 'v1.3/leads_call_virtual_number/get';
  137. $common_parameters = array(
  138. 'access_token' => $token,
  139. 'timestamp' => time(),
  140. 'nonce' => str_rand(10),
  141. );
  142. $parameters = [
  143. 'account_id' => intval($tmp['advertiser_id']),
  144. 'leads_id' => intval($tmp['clue_id']),
  145. 'caller' => $tmp['caller_number'],
  146. 'callee' => $tmp['callee_number'],
  147. 'request_id' => $tmp['request_id']
  148. ];
  149. $request_url = $url . '?' . http_build_query($common_parameters);
  150. $output = $this->getTxCurl($request_url, $parameters);
  151. return json_decode($output, true);
  152. }
  153. /**
  154. * curl请求
  155. */
  156. public function getCurl($url, $info, $headers)
  157. {
  158. $args = [];
  159. foreach ($info as $key => $value) {
  160. $args[$key] = is_string($value) ? $value : json_encode($value);
  161. }
  162. $url = $url . "?" . http_build_query($args);
  163. $ch = curl_init($url);
  164. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  165. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  166. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  167. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  168. $output = curl_exec($ch);
  169. curl_close($ch);
  170. return json_decode($output, true);
  171. }
  172. /**
  173. * 腾讯curl
  174. */
  175. public function getTxCurl($request_url,$parameters)
  176. {
  177. $curl = curl_init();
  178. curl_setopt($curl, CURLOPT_URL, $request_url);
  179. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
  180. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  181. curl_setopt($curl, CURLOPT_POST, 1);
  182. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
  183. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  184. "Content-Type:application/json"
  185. ));
  186. $response = curl_exec($curl);
  187. curl_close($curl);
  188. return $response;
  189. }
  190. }