appid = $appid; $this->secret = $secret; } /** * 生成授权地址 */ public function authorizedAddress($root_id) { return "https://developers.e.qq.com/oauth/authorize?client_id=" . $this->appid . "&state=" . $root_id . "&redirect_uri=" . urlencode($this->cakkback); } /** * 获取token和refresh_token */ public function getAccessToken($code) { $url = $this->domain . 'oauth/token'; $param = [ 'client_id' => intval($this->appid), 'client_secret' => $this->secret, 'grant_type' => 'authorization_code', 'authorization_code' => $code, 'redirect_uri' => $this->cakkback ]; return $this->getCurl($url, $param, ['Content-Type:' => 'application/json']); } /** * 获取腾讯线索 */ public function getClueListData($token, $advertiser_ids, $start_time, $end_time, $page, $page_size) { $url = $this->domain . '/v1.3/lead_clues/get'; $common_parameters = [ 'access_token' => $token, 'timestamp' => time(), 'nonce' => str_rand(10), ]; $parameters = [ 'account_id' => intval($advertiser_ids), //单一获取(多个广告主需要循环) 'time_range' => [ 'start_time' => $start_time, //时间戳 'end_time' => $end_time, //时间戳 ], 'page' => $page, 'page_size' => $page_size, ]; $request_url = $url . '?' . http_build_query($common_parameters); $output = $this->getTxCurl($request_url, $parameters); return json_decode($output, true); } /** * 获取广告组数据-每天获取一次上一天的数据保存到数据库 */ function getCampaignList($token,$ad,$time) { $url = $this->domain . '/v1.3/daily_reports/get'; $common_parameters = array( 'access_token' => $token, 'timestamp' => time(), 'nonce' => str_rand(10), ); $parameters = [ 'account_id' => intval($ad), 'level' => 'REPORT_LEVEL_ADGROUP', 'date_range' => [ 'start_date' => $time, 'end_date' => $time, ], 'group_by' => ['date','adgroup_id'], 'page' => 1, 'page_size' => 1000, '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'] ]; $parameters = array_merge($common_parameters, $parameters); return $this->getCurl($url, $parameters,['Content-Type:' => 'application/json']); } /** * 获取广告组下的详细 */ function getCampaignAdList($token,$ad,$time,$gid) { $url = $this->domain . '/v1.3/daily_reports/get'; $common_parameters = array( 'access_token' => $token, 'timestamp' => time(), 'nonce' => str_rand(10), ); $parameters = [ 'account_id' => intval($ad), 'level' => 'REPORT_LEVEL_AD', 'date_range' => [ 'start_date' => $time, 'end_date' => $time, ], "filtering" => [ [ "field" => 'adgroup_id', 'operator' => 'EQUALS', 'values' => [$gid] ] ], 'group_by' => ['date', 'ad_id'], 'page' => 1, 'page_size' => 1000, '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'] ]; $parameters = array_merge($common_parameters, $parameters); return $this->getCurl($url, $parameters, ['Content-Type:' => 'application/json']); } /** * 获取中间号 */ public function getCallVirtualNumber($token, $tmp) { $url = $this->domain . 'v1.3/leads_call_virtual_number/get'; $common_parameters = array( 'access_token' => $token, 'timestamp' => time(), 'nonce' => str_rand(10), ); $parameters = [ 'account_id' => intval($tmp['advertiser_id']), 'leads_id' => intval($tmp['clue_id']), 'caller' => $tmp['caller_number'], 'callee' => $tmp['callee_number'], 'request_id' => $tmp['request_id'] ]; $request_url = $url . '?' . http_build_query($common_parameters); $output = $this->getTxCurl($request_url, $parameters); return json_decode($output, true); } /** * curl请求 */ public function getCurl($url, $info, $headers) { $args = []; foreach ($info as $key => $value) { $args[$key] = is_string($value) ? $value : json_encode($value); } $url = $url . "?" . http_build_query($args); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } /** * 腾讯curl */ public function getTxCurl($request_url,$parameters) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $request_url); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Content-Type:application/json" )); $response = curl_exec($curl); curl_close($curl); return $response; } }