How to integrate the affiliate panel in your website using API

How to integrate the affiliate panel in your website using API

Did you know that you can easily integrate the affiliate panel in your website? It can be done using the API provided with our affiliate software. 

In this tutorial we will create a simple page that will work as standalone affiliate panel. It will show the tracking link of the affiliate and the total of commissions he has earned. If the user is not registered in your affiliate network, he will be able to sign up as affiliate with one single click.

The only requirement you need is to have the names and the email addresses of your users. Therefore, we suggest placing the below code in a section of your website where your users are already logged in, so you can easily get the user’s name and email.


API calls

The API calls that are used for this tutorial are the following:

1. Read the affiliate data (to check if the user is registered)

2. Sign up an affiliate (if the user is not registered) 

3. Read the total statistics;


Code

This image is an example of final result: 

Affiliate panel API

And here is the full code:

<?php

// AffiliationSoftware API example


// change these with your affiliationsoftware URL and API

$your_url = 'https://NAME.affiliationsoftware.app/';

$your_api = 'https://NAME.affiliationsoftware.app/script/api.php?key=XXX';


// change these with your users details

$user_name = "John Doe";

$user_email = "test@test.test";


// read affiliate data to check if the user is already registered

$data = array(

  'action' => 'affiliate_data',

  'affiliate' => $user_email,

);

$api = curl_init( $your_api );

curl_setopt( $api, CURLOPT_POST, 1 );

curl_setopt( $api, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt( $api, CURLOPT_POSTFIELDS, $data );

$res = curl_exec( $api );

curl_close( $api );

$affiliate = json_decode( $res, true );


// if the user is already registed

if( isset( $affiliate['refid'] ) ) {


// default affiliate link

$affiliate['link'] = $your_url . $affiliate['refid'];


// read total statistics

$data = array(

  'action' => 'affiliate_stats',

  'affiliate' => $user_email,

);

$api = curl_init( $your_api );

curl_setopt( $api, CURLOPT_POST, 1 );

curl_setopt( $api, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt( $api, CURLOPT_POSTFIELDS, $data );

$res = curl_exec( $api );

curl_close( $api );

$statistics = json_decode( $res, true );


// if user is not registred

if( !isset( $affiliate['refid'] ) && isset( $_GET['signup'] ) ) {

// signup affiliate

$data = array(

  'action' => 'affiliate_add',

  'name' => $user_name,

  'email' => $user_email, 

);

$api = curl_init( $your_api );

curl_setopt( $api, CURLOPT_POST, 1 );

curl_setopt( $api, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt( $api, CURLOPT_POSTFIELDS, $data );

$res = curl_exec( $api );

curl_close( $api );

$signup = json_decode( $res, true );


// reload page after signup

if( isset( $signup['success'] ) ) { header( 'location: ?' ); exit; }
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AffiliationSoftware API example</title>
</head>
<body>

<h1>Affiliate program</h1>

<?php if( isset( $affiliate['refid'] ) ) { ?>

<h3>Your affiliate link</h3>

<p><?php echo $affiliate['link']; ?></p>

<h3>Your statistics</h3>

<ul>

<li>Pending commissions: <strong><?php echo $statistics['transactions_pending']; ?> €</strong></li>

<li>Approved commissions: <strong><?php echo $statistics['transactions_approved']; ?> €</strong></li>

<li>Paid commissions: <strong><?php echo $statistics['transactions_paid']; ?> €</strong></li>

</ul>

<?php } else { ?> 

<h3>Sign up as affiliate</h3>

<p><a href='?signup'>Click to join</a></p>

<?php } ?>

</body>
</html>

Feel free to use it for your project but don't forget to replace the URL and the secret API key with your own. You can find your API under: Admin panel > settings > AffiliationSoftware.

Of course this is a basic example, but you can improve it with other functions, and you can easily implement your own design as well.

Article published on: 01.09.2021

Try now AffiliationSoftware, free for 30 days

Create your own Affiliate Network with the #1 Affiliate Software in Italy & Switzerland

Start now

We work well with