使用flipkart api获取特定单个产品的价格,标题和其他详细信息

时间:2015-06-07 07:57:28

标签: php mysql json api base64

我正在使用clusterdev.flipkart-api,从这个api我得到了flipkart目录名和链接然后点击它我得到了目录的可用产品。 但我需要从这个api中获取特定产品的价格,标题,inStock详细信息。我试图做一些改变,但我对json没有更多的了解,所以我无法获得细节。 获得特定产品的价格,标题,inStock详细信息后,我需要将其更新到我的数据库中。

Flipkart Api常见问题链接:http://www.flipkart.com/affiliate/apifaq

请建议我为获得产品的价格,头衔,产品和其他细节需要做出哪些更改。

实际代码:https://github.com/xaneem/flipkart-api-php 包含的文件:https://github.com/xaneem/flipkart-api-php/blob/master/clusterdev.flipkart-api.php

此处代码

<?php

//Include the class.
include "clusterdev.flipkart-api.php";

//Replace <affiliate-id> and <access-token> with the correct values
$flipkart = new \clusterdev\Flipkart("pratikson3", "853d3bc027514b3aa33f1caa4f30f1cf", "json");


//To view category pages, API URL is passed as query string.
$url = isset($_GET['url'])?$_GET['url']:false;
if($url){
//URL is base64 encoded to prevent errors in some server setups.
$url = base64_decode($url);

//This parameter lets users allow out-of-stock items to be displayed.
$hidden = isset($_GET['hidden'])?false:true;

//Call the API using the URL.
$details = $flipkart->call_url($url);

if(!$details){
    echo 'Error: Could not retrieve products list.';
    exit();
}

//The response is expected to be JSON. Decode it into associative arrays.
$details = json_decode($details, TRUE);

//The response is expected to contain these values.
$nextUrl = $details['nextUrl'];
$validTill = $details['validTill'];
$products = $details['productInfoList'];

//Products table
echo "<table border=2 cellpadding=10 cellspacing=1 style='text-align:center'>";
$count = 0;
$end = 1;

//Make sure there are products in the list.
if(count($products) > 0){
    foreach ($products as $product) {

        //Hide out-of-stock items unless requested.
        $inStock = $product['productBaseInfo']['productAttributes']['inStock'];
        if(!$inStock && $hidden)
            continue;

        //Keep count.
        $count++;

        //The API returns these values nested inside the array.
        //Only image, price, url and title are used in this demo
        $productId = $product['productBaseInfo']['productIdentifier']['productId'];
        $title = $product['productBaseInfo']['productAttributes']['title'];
        $productDescription = $product['productBaseInfo']['productAttributes']['productDescription'];

        //We take the 200x200 image, there are other sizes too.
        $productImage = array_key_exists('200x200', $product['productBaseInfo']['productAttributes']['imageUrls'])?$product['productBaseInfo']['productAttributes']['imageUrls']['200x200']:'';
        $sellingPrice = $product['productBaseInfo']['productAttributes']['sellingPrice']['amount'];
        $productUrl = $product['productBaseInfo']['productAttributes']['productUrl'];
        $productBrand = $product['productBaseInfo']['productAttributes']['productBrand'];
        $color = $product['productBaseInfo']['productAttributes']['color'];
        $productUrl = $product['productBaseInfo']['productAttributes']['productUrl'];

        //Setting up the table rows/columns for a 3x3 view.
        $end = 0;
        if($count%3==1)
            echo '<tr><td>';
        else if($count%3==2)
            echo '</td><td>';
        else{
            echo '</td><td>';
            $end =1;
        }

        echo '<a target="_blank" href="'.$productUrl.'"><img src="'.$productImage.'"/><br>'.$title."</a><br>Rs. ".$sellingPrice;

        if($end)
            echo '</td></tr>';

    }
}

//A message if no products are printed. 
if($count==0){
    echo '<tr><td>The retrieved products are not in stock. Try the Next button or another category.</td><tr>';
}

//A hack to make sure the tags are closed.  
if($end!=1)
    echo '</td></tr>';

echo '</table>';

//Next URL link at the bottom.
echo '<h2><a href="?url='.base64_encode($nextUrl).'">NEXT >></a></h2>';

//That's all we need for the category view.
exit();
}



//Category Selection Page
//If the control reaches here, the API directory view is shown.

//Query the API
$home = $flipkart->api_home();

//Make sure there is a response.
if($home==false){
    echo 'Error: Could not retrieve API homepage';
    exit();
}

//Convert into associative arrays.
$home = json_decode($home, TRUE);

$list = $home['apiGroups']['affiliate']['apiListings'];

//Create the tabulated view for different categories.
echo '<table border=2 style="text-align:center;">';
$count = 0;
$end = 1;
foreach ($list as $key => $data) {
    $count++;
    $end = 0;

    //To build a 3x3 table.
    if($count%3==1)
        echo '<tr><td>';
    else if($count%3==2)
        echo '</td><td>';
else{
    echo '</td><td>';
    $end =1;
}

echo "<strong>".$key."</strong>";
echo "<br>";
//URL is base64 encoded when sent in query string.
echo '<a href="?url='.base64_encode($data['availableVariants']['v0.1.0']['get']).'">View Products &raquo;</a>';
}

if($end!=1)
    echo '</td></tr>';
echo '</table>';

//This was just a rough example created in limited time.
//Good luck with the API.

3 个答案:

答案 0 :(得分:6)

<?php

    //Include the class.

    include "clusterdev.flipkart-api.php";

    //Replace <affiliate-id> and <access-token> with the correct values

    $flipkart = new \clusterdev\Flipkart("pratikson3", "853d3bc027514b3aa33f1caa4f30f1cf", "json");

    $pid = "Flipkart Product ID";

    $url = 'https://affiliate-api.flipkart.net/affiliate/product/json?id=' .$pid;

    //Call the API using the URL.

    $details = $flipkart->call_url($url);

    if(!$details){

        echo 'Error: Could not retrieve products list.';

        exit();
    }

    //The response is expected to be JSON. Decode it into associative arrays.

    $details = json_decode($details, TRUE);

    $price = $details['productBaseInfo']['productAttributes']['sellingPrice']['amount'];

    $inStock = (int) $details['productBaseInfo']['productAttributes']['inStock'];

    $title = $details['productBaseInfo']['productAttributes']['title'];

    $description = $details['productBaseInfo']['productAttributes']['productDescription'];

答案 1 :(得分:0)

试试这个,

<?php

$newStr = 'hairpin'; // Your search query
$url = 'https://affiliate-api.flipkart.net/affiliate/search/json?query='.$newStr.'&resultCount=1';

$pid = "COMEAE7XDPMSAMRW"; // Provide our product ID here
$urlNew = 'https://affiliate-api.flipkart.net/affiliate/product/json?id=' .$pid;

        //The headers are required for authentication
        $headers = array(
                'Cache-Control: no-cache',
                'Fk-Affiliate-Id: '.'xxxxxx',// Your flipcart affilicate ID
                'Fk-Affiliate-Token: '.'231324rf3r3434f'// 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, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);        
        $json = json_decode($result);       
        $allResults = $json->productInfoList;

    var_dump($allResults[0]->productBaseInfo->productAttributes);

这对我有用,它提供了以下回复。

object(stdClass)[7]
  public 'title' => string 'I Jewels Pearl With Jhumka Hair Pin' (length=35)
  public 'productDescription' => string 'I Jewels Traditional Gold Plated Elegantly Handcrafted Pearl with Jhumka Hair Pin for Women T1013W (White)' (length=106)
  public 'imageUrls' => 
    object(stdClass)[8]
      public '200x200' => string 'http://img.fkcdn.com/image/hair-accessory/s/n/h/t1013w-1-i-jewels-pearl-with-jhumka-200x200-imaeevhbgq9yjuxy.jpeg' (length=113)
      public '400x400' => string 'http://img.fkcdn.com/image/hair-accessory/s/n/h/t1013w-1-i-jewels-pearl-with-jhumka-400x400-imaeevhbgq9yjuxy.jpeg' (length=113)
      public '800x800' => string 'http://img.fkcdn.com/image/hair-accessory/s/n/h/t1013w-1-i-jewels-pearl-with-jhumka-800x800-imaeevhbgq9yjuxy.jpeg' (length=113)
      public 'unknown' => string 'http://img.fkcdn.com/image/hair-accessory/s/n/h/t1013w-1-i-jewels-pearl-with-jhumka-original-imaeevhbgq9yjuxy.jpeg' (length=114)
  public 'maximumRetailPrice' => 
    object(stdClass)[9]
      public 'amount' => float 1100
      public 'currency' => string 'INR' (length=3)
  public 'sellingPrice' => 
    object(stdClass)[10]
      public 'amount' => float 399
      public 'currency' => string 'INR' (length=3)
  public 'productUrl' => string 'http://dl.flipkart.com/dl/jewels-pearl-jhumka-hair-pin/p/itmeevzzwhmvbksy?pid=HACEEVZZHNVMRSNH&affid=sukeshini' (length=110)
  public 'productBrand' => string 'I Jewels' (length=8)
  public 'inStock' => boolean true
  public 'isAvailable' => boolean true
  public 'codAvailable' => boolean true
  public 'emiAvailable' => null
  public 'discountPercentage' => float 63
  public 'cashBack' => null
  public 'offers' => 
    array (size=0)
      empty
  public 'size' => null
  public 'color' => string '' (length=0)
  public 'sizeUnit' => string '' (length=0)
  public 'sizeVariants' => string '[HACEEVZZHNVMRSNH]' (length=18)
  public 'colorVariants' => string '[]' (length=2)
  public 'styleCode' => null

希望这有帮助。

答案 2 :(得分:0)

尝试一下

Search API - https://affiliate-api.flipkart.net/affiliate/1.0/search.json?query=laptop

product Api - https://affiliate-api.flipkart.net/affiliate/1.0/product.json?id=MOBFK4REY6GGXSAY

https://www.makemyshop.in/