php对接TopOn广告收益reporting api分享

本文介绍了如何通过Topon广告平台的reportingapi接口获取广告收益,包括获取接口文档、获取后台APIKey、设置请求参数和请求头,以及执行请求的示例代码。
摘要由CSDN通过智能技术生成
今天需要接入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;
    }

代码将指定的参数换成自己的参数之后,即可直接使用,拿到对应的返回数据之后再根据我们自己的业务需求去做,希望能对你有所帮助。

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大力水手z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值