Skip to main content
All CollectionsIntegrations
Postback instruction for PHP
Postback instruction for PHP

There's a guide for clients who need to implement the Postback integration on PHP. 

Anna Holovashchenko avatar
Written by Anna Holovashchenko
Updated over a year ago

Each request has to have a header with the user's authentication key.

X-Api-Key: #Your key#

You can find and manage the X-Api-Key in your account settings.

Your X-Api-Key - has to be saved in secure storage (e.g. database, environment variables).

Basic concepts

Defining constants for further usability.

define('TAP_COOKIE_DAYS', 90); // Number of days cookies are stored for
define('TAP_COOKIE_NAME', 'tap_data'); // Cookie name
define('TAP_CONVERSION_POSTBACK_URL', 'https://api.tapfiliate.com/1.7/pb/con/c/'); // Link to the endpoint for working with conversions
define('TAP_CUSTOMER_POSTBACK_URL', 'https://api.tapfiliate.com/1.7/pb/cus/c/'); // Link to the endpoint for working with customers

For parsing data from global arrays when going to the site, the desired transition parameters need to be stored in the user's cookies.

function parseAndSaveDataOnClick()
{
$resultArray = [];

if ($assetData = $_GET['aid'] ?? $_GET['asset_id'] ?? $_GET['a'] ?? $_POST['aid'] ?? $_POST['asset_id'] ?? $_POST['a']) {
$resultArray['asset_id'] = $assetData;
}

if ($sourceData = $_GET['sid'] ?? $_GET['source_id'] ?? $_GET['s'] ?? $_POST['sid'] ?? $_POST['source_id'] ?? $_POST['s']) {
$resultArray['source_id'] = $sourceData;
}

if ($refData = $_GET['refc'] ?? $_GET['referral_code'] ?? $_GET['ref'] ?? $_POST['refc'] ?? $_POST['referral_code'] ?? $_POST['ref']) {
$resultArray['referral_code'] = $refData;
}

setcookie(TAP_COOKIE_NAME, json_encode($resultArray), time() + 60 * 60 * 24 * TAP_COOKIE_DAYS, '/');
}

When a target action happens a customer needs to be created and a conversion tracked.

Create a customer

To create a customer, an array with data in it needs to be passed.

Required parameters:

customer id The unique identifier of the customer in your system.

You have to also send one of the parameters:

referral code An affiliate’s referral code.

coupon An affiliate’s coupon code.

asset id and source id A source id or an asset id.

Those parameters that come in the link will be taken from it and saved in cookies, the rest must be entered manually.

function createCustomer($customerData)
{
$requestData = array_merge(
json_decode($_COOKIE[TAP_COOKIE_NAME], true),
$customerData
);

return sendRequestToApi(TAP_CUSTOMER_POSTBACK_URL, $requestData);
}

Сreate a conversion

To create a conversion an array with data in it needs to be passed.

Required parameters:

external id A unique id for this conversion.

You have to also send one of the parameters:

referral code An affiliate’s referral code.

coupon An affiliate’s coupon code.

asset id and source id A source id or an asset id.

Those parameters that are included in the link will be taken from it and saved in cookies, the rest must be entered manually.

function createConversion($conversionData)
{
$requestData = array_merge(
json_decode($_COOKIE[TAP_COOKIE_NAME], true),
$conversionData
);

return sendRequestToApi(TAP_CONVERSION_POSTBACK_URL, $requestData);
}

After that, a customer with a conversion attached to it should appear in your Tapfiliate interface.

Example function for sending POSTBACK.

To create customers/conversions, an array with data in it needs to be passed.

function sendRequestToApi($url = '', $data = [])
{
$defaultHeaders = [
'X-Api-Key: ' . getenv('TAPFILIATE_API_KEY'), // Api-Key для доступа к API (taken from environment variables in this case)
'Content-type: application/json',
'Accept: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $defaultHeaders);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}

return $response;
}

Note: S2S/Postback tracking is an advanced feature. As such, set up of this feature falls outside the scope of our support. You may need a developer to assist in setting this up.

Did this answer your question?