-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.php
122 lines (101 loc) · 3.74 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
error_reporting(0);
//@ FutureRulers
//Include the class.
include "clusterdev.flipkart-api.php";
if(isset($_POST['search']))
{
$search=$_POST['search'];
$search=str_replace(" ", "+", $search);
//Replace <affiliate-id> and <access-token> with the correct values
$flipkart = new \Futurerulers\Flipkart("saikrishn32", "fe2cac2932dd4d199f754f1003243fa3", "json");
//To view category pages, API URL is passed as query string.
$url ='https://affiliate-api.flipkart.net/affiliate/search/json?query='.
$url.=$search.'&resultCount=10';
if($url){
//URL is base64 encoded to prevent errors in some server setups.
//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 width=1080 background-color=white align=center centerstyle='text-align:center' target=two>";
$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++;
$title = $product['productBaseInfo']['productAttributes']['title'];
// if($title=='/^American/')
if (preg_match("//i", $title))
{
//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 align="center"><td>';
else if($count%3==2)
echo '</td align="center"><td>';
else{
echo '</td align="center"><td>';
$end =1;
}
if($sellingPrice > $minp && $sellingPrice < $maxp)
{
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>';
//That's all we need for the category view.
exit();
}
else
{
echo out;
}//Query the API
$home = $flipkart->api_home();
//Make sure there is a response.
if($home==false){
echo 'Error: Could not retrieve API homepage';
exit();
}
}
?>