PHP: Get the Product Price using Flipkart API

Updated on     Kisan Patel

code
<?php

class Flipkart {
 
    function sendRequest($url, $timeout=30){
        //Make sure cURL is available
        //The headers are required for authentication
        $headers = array(
           'Cache-Control: no-cache',
           'Fk-Affiliate-Id: '.'YOUR_AFFILIATE_ID',
           'Fk-Affiliate-Token: '.'YOUR_AFFILIATE_TOKEN'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-ClusterDev-Flipkart/0.1');
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result ? $result : false;
   }
 
   function getFlipkartPrice($product_id){
       //To view category pages, API URL is passed as query string.
       //MOBDVHC6XKKPZ3GZ
       $url = 'https://affiliate-api.flipkart.net/affiliate/product/json?id=' . $product_id;
       $reponse = $this->sendRequest($url);
 
       if($reponse){
            $json_result = json_decode($reponse);
            $price = $json_result->productBaseInfo->productAttributes->sellingPrice->amount;
            $result = array(
                 "price" => $price
            );
            return $result;
       }
       else{
            return false;
       }
    }
}
?>

PHP

Leave a Reply