123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- namespace clue;
- /**
- * 飞鱼线索处理
- */
- class Fish
- {
- /**
- * 开发者应用ID及密钥
- */
- protected $appid;
- protected $secret;
- /**
- * 接口地址
- */
- protected $domain = 'https://ad.oceanengine.com/';
- /**
- * 授权回调地址
- */
- protected $cakkback = 'https://wzh.nczyzs.com/api/fish/fish_call_back/';
-
- public function __construct($appid,$secret)
- {
- $this->appid = $appid;
- $this->secret = $secret;
- }
- /**
- * 生成授权地址
- */
- public function authorizedAddress($root_id)
- {
- return $this->domain . "openapi/audit/oauth.html?app_id=" . $this->appid . "&material_auth=1&state=" . $root_id . "&redirect_uri=" . urlencode($this->cakkback);
- }
- /**
- * 获取token和refresh_token
- */
- public function getAccessToken($code)
- {
- $url = $this->domain . 'open_api/oauth2/access_token/';
- $param = [
- 'app_id' => intval($this->appid),
- 'secret' => $this->secret,
- 'grant_type' => 'auth_code',
- 'auth_code' => $code,
- ];
- return $this->curl($url, $param, ['Content-Type:' => 'application/json']);
- }
- /**
- * 获取线索列表
- */
- public function getClueListData($token, $advertiser_ids, $start_time, $end_time, $page, $page_size = 10)
- {
- //时间格式yyyy-MM-dd 或者 yyyy-MM-dd hh:mm:ss
- $value = explode(',',$advertiser_ids);
- $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;
- $headers = array(
- 'Access-Token: ' . $token,
- 'X-Debug-Mode: 1'
- );
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取已授权账户根据Token
- */
- public function getAdvertiser($token)
- {
- $url = $this->domain . 'open_api/2/user/info/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- return $this->getCurl($url, $headers);
- }
- /**
- * 刷新token
- */
- public function getRefreshToken($token)
- {
- $url = $this->domain . 'open_api/oauth2/refresh_token/';
- $param = [
- 'app_id' => intval($this->appid),
- 'secret' => $this->secret,
- 'grant_type ' => 'refresh_token',
- 'refresh_token' => $token,
- ];
- return $this->curl($url, $param, ['Content-Type:' => 'application/json']);
- }
- /**
- * 获取线索外呼中间号(AXB呼叫能力)
- */
- public function getCallVirtualNumber($token,$info)
- {
- $url = $this->domain . 'open_api/2/tools/clue/call_virtual_number/get/';
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 发起语音双呼(双呼呼叫能力)
- */
- public function createBridgeCall($token,$info)
- {
- $url = $this->domain . 'open_api/2/tools/clue/bridge_call/create/';
- $headers = array('Access-Token: ' . $token,'Content-Type: application/json');
- return $this->curl($url,json_encode($info), $headers);
- }
- /**
- * 获取广告主数据
- */
- public function getAdvertiserList($token,$param)
- {
- $url = $this->domain . 'open_api/2/report/advertiser/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => $param['advertiser_id'],//广告主ID
- 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
- 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
- 'page' => $param['page'],
- 'page_size' => $param['page_size'],
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取广告组下广告计划数据
- */
- public function getAdList($token,$param)
- {
- $url = $this->domain . 'open_api/2/report/ad/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => $param['advertiser_id'],//广告主ID
- 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
- 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
- 'filtering' => [
- 'campaign_ids' => [$param['campaign_ids']],//广告组id列表:按照campaign_id过滤,最多支持100个
- ],
- 'group_by'=>['STAT_GROUP_BY_FIELD_ID'],
- 'page' => $param['page'],
- 'page_size' => 100,
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取广告组数据-每天获取一次上一天的数据保存到数据库
- */
- public function getCampaignList($token,$param)
- {
- $url = $this->domain . 'open_api/2/report/campaign/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => $param['advertiser_id'],//广告主ID
- 'start_date' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
- 'end_date' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
- 'page' => $param['page'],
- 'group_by'=>['STAT_GROUP_BY_FIELD_ID'],
- 'page_size' => 100,
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取自定义报表可用指标和维度
- */
- public function getReportCustomConfig($token,$param)
- {
- $url = $this->domain . 'open_api/v3.0/report/custom/config/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => intval($param['advertiser_id']),
- 'data_topics' => ['BASIC_DATA']
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
-
- /**
- * 升级版获取广告组数据
- */
- public function getCampaignListV2($token,$param)
- {
- $url = $this->domain . 'open_api/v3.0/report/custom/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => $param['advertiser_id'],//广告主ID
- 'data_topic' => 'BASIC_DATA',
- 'dimensions' => ['stat_time_day','cdp_project_id','cdp_project_name'],
- 'start_time' => $param['start_date'],//起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
- 'end_time' => $param['end_date'],//结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
- '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'],
- 'filters' => [],
- 'order_by' => [],
- 'page' => $param['page'],
- 'page_size' => 100,
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取广告组下广告计划数据
- */
- public function getAdListV2($token,$param)
- {
- $url = $this->domain . 'open_api/v3.0/report/custom/get/';
- $headers = array(
- 'Access-Token: ' . $token
- );
- $info = [
- 'advertiser_id' => $param['advertiser_id'], //广告主ID
- 'data_topic' => 'BASIC_DATA',
- 'dimensions' => ['stat_time_day', 'cdp_promotion_id', 'cdp_promotion_name'],
- 'start_time' => $param['start_date'], //起始日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期
- 'end_time' => $param['end_date'], //结束日期,格式YYYY-MM-DD,只支持查询2016-10-26及以后的日期,时间跨度不能超过30天
- 'metrics' => ['stat_cost', 'show_cnt', 'cpm_platform', 'click_cnt', 'cpc_platform', 'ctr', 'convert_cnt', 'conversion_cost', 'conversion_rate'],
- 'filters' => [
- [
- 'field' => 'cdp_project_id',
- 'type' => 1,
- 'operator' => 1,
- 'values' => [$param['campaign_ids']]
- ]
- ],
- 'order_by' => [
- [
- 'field' => 'stat_cost',
- 'type' => 'ASC'
- ]
- ],
- 'page' => $param['page'],
- 'page_size' => 100,
- ];
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * 获取线索外呼录音文件下载链接
- */
- public function getClueRecordUrl($token,$info)
- {
- $url = $this->domain . 'open_api/2/tools/clue/contact_log/record_url/get/';
- $args = [];
- foreach ($info as $key => $value) {
- $args[$key] = is_string($value) ? $value : json_encode($value);
- }
- $url = $url . "?" . http_build_query($args);
- $headers = array('Access-Token: ' . $token,);
- return $this->getCurl($url, $headers);
- }
- /**
- * curl请求下get数据
- */
- public function getCurl($url, $headers)
- {
- $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 curl($url, $param, $header)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HEADER, false);
- $rs = curl_exec($curl);
- curl_close($curl);
- return json_decode($rs,true);
- }
- }
|