今天需要接入Topon广告平台收益,对接reporting api接口,拉取广告收益回来。分享给大家
首先找到接口文档地址:https://newdocs.toponad.com/docs/dukFyc
先看使用说明,需要去后台拿取对应的key:
登录到topon后台之后,点击右上角账号管理,进入到此页面就能看到相应的key,拿到参数就可以开始对接了,这里我们需要根据自己的需要列出指定的维度,返回数据的指标,我们可以全部返回,则填入all,其他参数根据文档填写即可
然后是请求头,文档有写接口鉴权说明,这里自己列出代码,给大家参考:
public function get_data($date)
{
$contentArr = [
'startdate' => (int)date('Ymd', strtotime($date)),
'enddate' => (int)date('Ymd', strtotime($date)),
'group_by' => ["date", "app", "placement", "adformat", "network_firm_id"],
'metric' => ["all"],
'start' => 0,
'limit' => 1000
];
$reqUrl = self::serverHost . "/v2/fullreport";
$response = json_decode($this->doRequest("POST", $contentArr, self::Api_Key, $reqUrl), true);
$data = $response['records'] ?? [];
if (empty($data)) {
return ['code' => 101, 'msg' => 'no data!'];
}
....业务逻辑开始
}
function doRequest($httpMethod, array $contentArr, $publisherKey, $reqUrl)
{
$time = time() * 1000;
$headerArr = [
'X-Up-Key' => $publisherKey,
'X-Up-Timestamp' => $time,
];
$headerStr = '';
foreach ($headerArr as $key => $value) {
if (empty($headerStr)) {
$headerStr = "$key:$value";
} else {
$headerStr = "$headerStr\n$key:$value";
}
}
if ($httpMethod == "GET") {
$reqUrl = $reqUrl . "?" . http_build_query($contentArr);
$contentStr = "";
} else {
$contentStr = json_encode($contentArr);
}
$contentMD5 = strtoupper(md5($contentStr));
$contentType = 'application/json';
$relativePath = parse_url($reqUrl)["path"];
$signStr = $httpMethod . "\n"
. $contentMD5 . "\n"
. $contentType . "\n"
. $headerStr . "\n"
. $relativePath;
$headerArr['X-Up-Signature'] = strtoupper(md5($signStr));;
$headerArr['Content-Type'] = $contentType;
return $this->execCurl($httpMethod, $reqUrl, $contentStr, $headerArr);
}
function execCurl($httpMethod, $reqUrl, $contentStr, array $headerArr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $reqUrl);
if ($httpMethod == 'GET') {
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
} elseif ($httpMethod == 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contentStr);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contentStr);
}
$finalHeaderArr = [];
foreach ($headerArr as $key => $value) {
$finalHeaderArr[] = $key . ":" . $value;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $finalHeaderArr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
代码将指定的参数换成自己的参数之后,即可直接使用,拿到对应的返回数据之后再根据我们自己的业务需求去做,希望能对你有所帮助。