今天对接的是Pangle广告reporting api接口,拉取广告收益回来自己做统计。记录分享给大家
首先是文档地址,进入到Pangle后台就能看到文档地址以及参数:
文档地址:https://www.pangleglobal.com/zh/integration/reporting-api-v2
在这里插入图片描述
接入这些第三方广告平台,流程基本上一样,拿好参数之后可以直接开始对接了,我直接贴代码出来:
$params = array(
"currency" => "usd",
"date" => $date,
"time_zone" => 0,
"timestamp" => time()
);
$url = pangle::get_media_rt_income_url('MD5', $params);
$client = new Client();
$response = $client->request('GET', $url);
$result = json_decode($response->getBody()->getContents(), true);
......下面是自己的逻辑代码
params 组装好参数,一个就是收益的计量单位,我们使用的是美元,还有就是时区
<?php
namespace App\Services\ThirdApi\lib;
final class pangle
{
private static $user_id = 2222; # Replace with your user_id
private static $role_id = 11111; # Replace with your role_id
private static $secure_key = "-11111; # Replace with your Secutiry Key
private static $version = "2.0";
const PANGLE_HOST = "https://open-api.pangleglobal.com";
const KEY_SIGN_TYPE = "sign_type";
const KEY_SIGN = "sign";
const KEY_ROLE_ID = "role_id";
const KEY_USER_ID = "user_id";
const KEY_VERSION = "version";
// Supported sign method
private static $sign_array = array(
'MD5' => 'sign_md5',
);
// md5 sign method
private static function sign_md5($str, $key)
{
$sign_str = $str . $key;
$sign = md5($sign_str);
#printf("md5 [sign string without key:%s] => [%s]", $sign_str,$sign);
return $sign;
}
// Examine sign method
private static function valid_sign_method($method)
{
if (empty(self::$sign_array[$method])) {
printf("unexpected sign method %s", $method);
return false;
}
return true;
}
// Sign the string
private static function sign($method, $str, $key)
{
if (!self::valid_sign_method($method)) {
return false;
}
return call_user_func(array('self', self::$sign_array[$method]), $str, $key);
}
// Sign the array
private static function make_sign($method, $arr_input, $sign_key)
{
// Config array to be signed
$sign_str = '';
foreach ($arr_input as $key => $val) {
$sign_str .= '&' . strval($key) . '=' . strval($val);
}
$sign_str = substr($sign_str, 1);
// Sign
$ret = self::sign($method, $sign_str, $sign_key);
return $ret;
}
// Create signed query string
private static function make_signed_querystring($method, $arr_input, $key, $key_sign = self::KEY_SIGN, $key_sign_method = self::KEY_SIGN_TYPE)
{
// Sign
$sign = self::make_signed_array_utf8($method, $arr_input, $key, $key_sign, $key_sign_method);
if (!$sign) {
return false;
}
// Create querystring
$query_string = http_build_query($arr_input);
return sprintf("%s&%s=%s", $query_string, $key_sign, $sign);
}
/**
* to retuen data in json,convert the sign result into utf-8
* @param sign_method, array to sign, secure_key, array key of sign and sign_method
* @return result of sign when success while false if not.
*/
private static function make_signed_array_utf8($method, &$arr_input, $key, $key_sign = self::KEY_SIGN, $key_sign_method = self::KEY_SIGN_TYPE)
{
// add the sign method into the array to be signed
$arr_input[$key_sign_method] = $method;
$arr_input[self::KEY_VERSION] = self::$version;
if (self::$role_id != "") {
$arr_input[self::KEY_ROLE_ID] = self::$role_id;
}
if (self::$user_id != "") {
$arr_input[self::KEY_USER_ID] = self::$user_id;
}
// Sort Array
if (!ksort($arr_input)) {
printf("make_signed_array_utf8::sort arr_input failed, arr: %s", printf($arr_input, true));
return false;
}
// create string to be signed
$signStr = '';
foreach ($arr_input as $inputKey => $inputVal) {
$signStr .= '&' . $inputKey . '=' . $inputVal;
}
$signStr = substr($signStr, 1);
// convert the string to utf-8
$signStr = mb_convert_encoding($signStr, 'UTF-8', 'GBK');
// Sign
$ret = self::sign($method, $signStr, $key);
if (!$ret) {
printf("make sign_utf8 failed, method:%s, str:%s", $method, print_r($arr_input, true));
return false;
}
return $ret;
}
static function get_media_rt_income_url($method, $arr_input, $key_sign = self::KEY_SIGN, $key_sign_method = self::KEY_SIGN_TYPE)
{
try {
$needDelArr = array();
foreach ($arr_input as $key => $value) {
if ($value === "") {
array_push($needDelArr, $key);
}
}
foreach ($needDelArr as $key => $value) {
unset($arr_input[$value]);
}
return self::PANGLE_HOST . "/union_pangle/open/api/rt/income?" . self::make_signed_querystring($method, $arr_input, self::$secure_key, $key_sign, $key_sign_method);
} catch (Exception $e) {
printf($e->getMessage());
return "";
}
}
static function set_secure_key($secure_key)
{
self::$secure_key = $secure_key;
}
static function set_user_id($user_id)
{
self::$user_id = $user_id;
}
static function set_role_id($role_id)
{
self::$role_id = $role_id;
}
}
这个是Pangle封装的类,我们直接使用即可
Pangle基本上参数组装之后,再用它的方法生成一个请求地址,然后GET请求就能拿到相应的数据,
请求参数有这些,大家根据自己的需求添加
下面是请求参数,有些是必选的,有些是过滤指标,相关参数,大家根据自己需要选择
支持的参数有下面这些
$model_type = [
'android' => 1,
'ios' => 2,
];
然后就是返回设备类型,安卓返回的是android,苹果返回的是ios,大家根据自己的需要存储
以上是我对接Pangle广告收益接口代码,欢迎大家交流学习,希望能对你有帮助。