Flipkart affiliate api in php

In this tutorial i am going to discuss about affiliate marketing of flipkart. First of all you should know about affiliate marketing. In india affiliate marketing growing day by day and people are earning very good amount of money from affiliate marketing.

What is affiliate marketing

Affiliate marketing is the process of earning a commission by promoting other people’s (or company’s) products. You find a product you like, promote it to others, and earn a piece of the commission for each sale that you make.
Or for more info about affiliate marketing you can google it.
So here i have created a flipkart affiliate api in php, So by using this API you can easily fetch flipkart offer and deals and display on your website.

Now Lets start the tutorial.

For using this api, You have to create flipkart affiliate account on https://affiliate.flipkart.com

After that you will able to generate your affiliate id and token to authenticate yourself during item request.

So here is the class file.



 class flipkartApi {
    private static $affiliateID;
    private static $token;
    private static $timeout = 45;
     public function __construct($affiliateID, $token) {
       self::$affiliateID = $affiliateID;
       self::$token = $token;
     public static function getData($url, $dataType) {
         try {
            if(!isset($url) && !empty($url)) {
                throw new exception("URL is not available.");
            if(!isset($dataType) && !empty($dataType)) {
                throw new exception("Please set datatype json or xml");
            if (!function_exists('curl_init')){
                throw new exception("Curl is not available.");
            $headers = array(
                'Cache-Control: no-cache',
                'Fk-Affiliate-Id: '.self::$affiliateID,
                'Fk-Affiliate-Token: '.self::$token
            $cObj = curl_init();
            curl_setopt($cObj, CURLOPT_URL, $url);
            curl_setopt($cObj, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($cObj, CURLOPT_TIMEOUT, self::$timeout);
            curl_setopt($cObj, CURLOPT_RETURNTRANSFER, TRUE);
            $result = curl_exec($cObj);
             if($dataType == 'json') {
               return $result ? json_decode($result, true) : false;
             } else if($dataType == 'xml') {
                return $result ? new SimpleXMLElement($result) : false;
             } else {
                return false;
         }  catch (Exception $e) {
            return $e->getMessage();

After that initialize flipkartApi class and call the getData function.
But before calling the function you have to pass two parameter url and datatype.
Here i am going to fetch flipkart offer in json format, You can get flipkart offer url from your affiliate account see attached image.

Pass these two parameter on getData function. Don’t forget to pass affilateID and token at the time of flipkartApi class inilization.


$affiliateID = 'YOUR_AFFILIATE_ID';
$token = 'YOUR_TOKEN';
$fkObj = new flipkartApi($affiliateID, $token);

$offerJsonURL =  'https://affiliate-api.flipkart.net/affiliate/offers/v1/all/json';
$result = flipkartApi::getData($offerJsonURL, 'json');

See live demo and download full source code.



If you like this post please don’t forget to subscribe my public notebook for more useful stuff