-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdemo.php
323 lines (245 loc) · 8.61 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* Demo Code
* PHP Wrapper for Flipkart API (unofficial)
* GitHub: https://github.com/xaneem/flipkart-api-php
* Demo: http://www.clusterdev.com/flipkart-api-demo
* License: MIT License
*
* @author Saneem (@xaneem, xaneem@gmail.com)
*/
//This is basic example code, and is not intended to be used in production.
//Don't forget to use a valid Affiliate Id and Access Token.
//Include the class.
include "clusterdev.flipkart-api.php";
//Replace <affiliate-id> and <access-token> with the correct values
$flipkart = new \clusterdev\Flipkart("<affiliate-id>", "<access-token>", "json");
$dotd_url = 'https://affiliate-api.flipkart.net/affiliate/offers/v1/dotd/json';
$topoffers_url = 'https://affiliate-api.flipkart.net/affiliate/offers/v1/top/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'];
//The navigation buttons.
echo '<h2><a href="?">HOME</a> | <a href="?url='.base64_encode($nextUrl).'">NEXT >></a></h2>';
//Message to be displayed if out-of-stock items are hidden.
if($hidden)
echo 'Products that are out of stock are hidden by default.<br><a href="?hidden=1&url='.base64_encode($url).'">SHOW OUT-OF-STOCK ITEMS</a><br><br>';
//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();
}
//Deal of the Day DOTD and Tops offers
$offer = isset($_GET['offer'])?$_GET['offer']:false;
if($offer){
if($offer == 'dotd'){
//Call the API using the URL.
$details = $flipkart->call_url($dotd_url);
if(!$details){
echo 'Error: Could not retrieve DOTD.';
exit();
}
//The response is expected to be JSON. Decode it into associative arrays.
$details = json_decode($details, TRUE);
$list = $details['dotdList'];
//The navigation buttons.
echo '<h2><a href="?">HOME</a> | DOTD Offers | <a href="?offer=topoffers">Top Offers</a></h2>';
//Show 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($list) > 0){
foreach ($list as $item) {
//Keep count.
$count++;
//The API returns these values
$title = $item['title'];
$description = $item['description'];
$url = $item['url'];
$imageUrl = $item['imageUrls'][0]['url'];
$availability = $item['availability'];
//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="'.$url.'"><img src="'.$imageUrl.'" style="max-width:200px; max-height:200px;"/><br>'.$title."</a><br>".$description;
if($end)
echo '</td></tr>';
}
}
//A message if no products are printed.
if($count==0){
echo '<tr><td>No DOTDs returned.</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 if($offer == 'topoffers'){
//Call the API using the URL.
$details = $flipkart->call_url($topoffers_url);
if(!$details){
echo 'Error: Could not retrieve Top Offers.';
exit();
}
//The response is expected to be JSON. Decode it into associative arrays.
$details = json_decode($details, TRUE);
$list = $details['topOffersList'];
//The navigation buttons.
echo '<h2><a href="?">HOME</a> | <a href="?offer=dotd">DOTD Offers</a> | Top Offers</h2>';
//Show 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($list) > 0){
foreach ($list as $item) {
//Keep count.
$count++;
//The API returns these values
$title = $item['title'];
$description = $item['description'];
$url = $item['url'];
$imageUrl = $item['imageUrls'][0]['url'];
$availability = $item['availability'];
//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="'.$url.'"><img src="'.$imageUrl.'" style="max-width:200px; max-height:200px;"/><br>'.$title."</a><br>".$description;
if($end)
echo '</td></tr>';
}
}
//A message if no products are printed.
if($count==0){
echo '<tr><td>No Top Offers returned.</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 'Error: Invalid offer type.';
exit();
}
}
//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'];
echo '<h1>API Homepage</h1><h2><a href="?offer=dotd">DOTD Offers</a> | <a href="?offer=topoffers">Top Offers</a></h2>Click on a category link to show available products from that category.<br><br>';
//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.