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.
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.
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.


* Author: Rohit Kumar
* Website:
* Version: 0.0.1
* Date: 7-02-2016
* App Name: Snapdeal Affiliate Api
* Description: Simple Snapdeal api in php to create your online deals or e-commerce affiliate market place 
 class snapdealApi {
    private static $affiliateID;
    private static $token;
    private static $timeout = 45;
    //Set snapdeal affilate id and token at the time of class init.
     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.");
         	 // Set header to make authentication
	        $headers = array(
	            '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);
             // render result as per format.
              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.

// Get affiliateID and token from
// Set snapdeal affiliateID and token
$affiliateID = 'YOUR_AFFILIATE_ID';
$token = 'YOUR_TOKEN';
$sdObj = new snapdealApi($affiliateID, $token);
$url =  '';
$result = snapdealApi::getData($url, '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

Posted in PHP