Snapdeal affiliate api in php

This tutorial about affiliate marketing of snapdeal products, Last month i have create tutorial on flipkart affiliate api to fetch flipkart products, Same process will be follow again but here i’ll show you how to fetch snapdeal products using snapdeal affiliate api.

If you are not aware about what is affiliate marketing then below is the sort definition..

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.
snapdeal-affiliate-1
For fetching snapdeal products i created a simple api in php, You only need to pass three parameter, Your affiliate id, Your token no. and Product feed url.




To generate snapdeal affiliate id and token goto snapdeal affiliate panel and create an account.
https://affiliate.snapdeal.com/
snapdeal-affiliate-2
Lets start the tutorial.

Create a php class file and write some curl operation, pass required header like snapdeal affiliate id, token no etc to authenticate your request.

snapdealApiClass.php

<?php

 class snapdealApi {
 
    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(
	            'Accept:application/'.$dataType,
	            'Snapdeal-Affiliate-Id: '.self::$affiliateID,
	            'Snapdeal-Token-Id: '.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);
	        curl_close($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 snapdealApi class and call the getData function.
But before calling the function you have to pass two parameter url and datatype.

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

Here first i’ll fetch all snapdeal product categories and if you’ll click any category then you’ll able see all associated products of that category.

<?php
include_once("snapdealApiClass.php");


$affiliateID = 'YOUR_AFFILIATE_ID';
$token = 'YOUR_TOKEN';
$sdObj = new snapdealApi($affiliateID, $token);
$url =  'http://affiliate-feeds.snapdeal.com/feed/72967.json';
$result = snapdealApi::getData($url, 'json');
var_dump($result);
?>

See live demo and download full source code.

DEMO

DOWNLOAD

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

Posted in PHP