使用 Flipkart api 获取特定单品的价格、标题和其他详细信息
Posted
技术标签:
【中文标题】使用 Flipkart api 获取特定单品的价格、标题和其他详细信息【英文标题】:particular single product's price, title and other detail fetch using flipkart api 【发布时间】:2015-08-21 20:48:14 【问题描述】:我正在使用 clusterdev.flipkart-api,从这个 api 我得到了 Flipkart 目录名称和链接,然后单击它我正在获取目录的可用产品。 但我需要从此 api 获取特定产品的价格、标题、库存详情。我曾尝试进行一些更改,但我没有更多关于 json 的知识,所以我无法获得详细信息。 在获得特定产品的价格、标题、库存详情后,我需要将其更新到我的数据库中。
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 »</a>';
if($end!=1)
echo '</td></tr>';
echo '</table>';
//This was just a rough example created in limited time.
//Good luck with the API.
【问题讨论】:
【参考方案1】:试试这个,
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/
【讨论】:
【参考方案2】:试试这个,
<?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
希望这会有所帮助。
【讨论】:
【参考方案3】:<?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'];
【讨论】:
我尝试进行更改,但没有任何反应,我是 json 新手,我不知道如何在上述查询中进行更改。你能建议我做些什么改变吗? 无需更改 clusterdev.flipkart-api.php 中的任何内容,只需在 demo.php/任何测试文件中进行更改即可调用 API。我已经编辑了我的代码以理解。 谢谢,我会试试的。并让您知道。以上是关于使用 Flipkart api 获取特定单品的价格、标题和其他详细信息的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Flipkart Seller GET LISTING API在SKU中获得带有正斜杠(/)的有效列表?