· 8 years ago · Aug 26, 2018, 04:52 PM
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5<title>Merchandising Tutorial Sample</title>
6<style type="text/css">body { font-family: arial,sans-serif; font-size: small; } </style>
7</head>
8<body>
9
10<?php
11 // Turn on all errors, warnings and notices for easier PHP debugging
12 error_reporting(E_ALL);
13
14 // Define global variables
15 $s_endpoint = "http://open.api.ebay.com/shopping?"; // Shopping URL to call
16 $cellColor = "bgcolor=\"#dfefff\""; // Light blue background used for selected items
17 $m_endpoint = 'http://svcs.ebay.com/MerchandisingService?hairbitstore'; // Merchandising URL to call
18 $appid = 'Francesc-Keivan-SBX-7b1d2c993-6a3fad93'; // You will need to supply your own AppID
19 $responseEncoding = 'XML'; // Type of response we want back
20
21 // Create a function for the getMostWatchedItems call
22 function getMostWatchedItemsResults ($selectedItemID = '', $cellColor = '') {
23 global $m_endpoint;
24 global $appid;
25 global $responseEncoding;
26
27 // Construct getMostWatchedItems call with maxResults and categoryId as input
28 $apicalla = "$m_endpoint";
29 $apicalla .= "OPERATION-NAME=getMostWatchedItems";
30 $apicalla .= "&SERVICE-VERSION=1.0.0";
31 $apicalla .= "&CONSUMER-ID=$appid";
32 $apicalla .= "&RESPONSE-DATA-FORMAT=$responseEncoding";
33 $apicalla .= "&maxResults=3";
34 $apicalla .= "&categoryId=293";
35
36 // Load the call and capture the document returned by eBay API
37 $resp = simplexml_load_file($apicalla);
38
39 // Check to see if the response was loaded, else print an error
40 if ($resp) {
41 // Set return value for the function to null
42 $retna = '';
43
44 // Verify whether call was successful
45 if ($resp->ack == "Success") {
46
47 // If there were no errors, build the return response for the function
48 $retna .= "<h1>Top 3 Most Watched Items in the ";
49 $retna .= $resp->itemRecommendations->item->primaryCategoryName;
50 $retna .= " Category</h1> \n";
51
52 // Build a table for the 3 most watched items
53 $retna .= "<!-- start table in getMostWatchedItemsResults --> \n";
54 $retna .= "<table width=\"100%\" cellpadding=\"5\" border=\"0\"><tr> \n";
55
56 // For each item node, build a table cell and append it to $retna
57 foreach($resp->itemRecommendations->item as $item) {
58
59 // Set the cell color blue for the selected most watched item
60 if ($selectedItemID == $item->itemId) {
61 $thisCellColor = $cellColor;
62 } else {
63 $thisCellColor = '';
64 }
65
66 // Determine which price to display
67 if ($item->currentPrice) {
68 $price = $item->currentPrice;
69 } else {
70 $price = $item->buyItNowPrice;
71 }
72
73 // For each item, create a cell with imageURL, viewItemURL, watchCount, currentPrice
74 $retna .= "<td $thisCellColor valign=\"bottom\"> \n";
75 $retna .= "<img src=\"$item->imageURL\"> \n";
76 $retna .= "<p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</a></p>\n";
77 $retna .= 'Watch count: <b>' . $item->watchCount . "</b><br> \n";
78 $retna .= 'Current price: <b>$' . $price . "</b><br><br> \n";
79 $retna .= "<FORM ACTION=\"" . $_SERVER['PHP_SELF'] . "\" METHOD=\"POST\"> \n";
80 $retna .= "<INPUT TYPE=\"hidden\" NAME=\"Selection\" VALUE=\"$item->itemId\"> \n";
81 $retna .= "<INPUT TYPE=\"submit\" NAME=\"$item->itemId\" ";
82 $retna .= "VALUE=\"Get Details and Related Category Items\"> \n";
83 $retna .= "</FORM> \n";
84 $retna .= "</td> \n";
85 }
86 $retna .= "</tr></table> \n<!-- finish table in getMostWatchedItemsResults --> \n";
87
88 } else {
89 // If there were errors, print an error
90 $retna = "The response contains errors<br>";
91 $retna .= "Call used was: $apicalla";
92
93 } // if errors
94
95 } else {
96 // If there was no response, print an error
97 $retna = "Dang! Must not have got the getMostWatchedItems response!<br>";
98 $retna .= "Call used was: $apicalla";
99 } // End if response exists
100
101 // Return the function's value
102 return $retna;
103
104 } // End of getMostWatchedItemsResults function
105
106
107 // Use itemId from selected most watched item as input for a GetSingleItem call
108 function getSingleItemResults ($selectedItemID) {
109 global $s_endpoint;
110 global $appid;
111 global $responseEncoding;
112 global $cellColor;
113
114 $retnb = '';
115
116 // Construct the GetSingleItem call
117 $apicallb = "$s_endpoint";
118 $apicallb .= "callname=GetSingleItem";
119 $apicallb .= "&version=563";
120 $apicallb .= "&appid=$appid";
121 $apicallb .= "&itemid=$selectedItemID";
122 $apicallb .= "&responseencoding=$responseEncoding";
123 $apicallb .= "&includeselector=Details,ShippingCosts";
124
125 // Load the call and capture the document returned by eBay API
126 $resp = simplexml_load_file($apicallb);
127
128 // Check to see if the response was loaded, else print an error
129 if ($resp) {
130
131 // If there is a response check for a picture of the item to display
132 if ($resp->Item->PictureURL) {
133 $picURL = $resp->Item->PictureURL;
134 } else {
135 $picURL = "http://pics.ebaystatic.com/aw/pics/express/icons/iconPlaceholder_96x96.gif";
136 }
137
138 // Check for shipping cost information
139 if ($resp->Item->ShippingCostSummary->ShippingServiceCost) {
140 $shippingCost = "\$" . $resp->Item->ShippingCostSummary->ShippingServiceCost;
141 } else {
142 $shippingCost = "Not Specified";
143 }
144
145 // Build a table of item and user details for the selected most watched item
146 $retnb .= "<!-- start table in getSingleItemResults --> \n";
147 $retnb .= "<table width=\"100%\" cellpadding=\"5\"><tr> \n";
148 $retnb .= "<td $cellColor width=\"50%\">\n";
149 $retnb .= "<div align=\"left\"> <!-- left align item details --> \n";
150 $retnb .= "Current price: <b>\$" . $resp->Item->ConvertedCurrentPrice . "</b><br> \n";
151 $retnb .= "Shipping cost: <b>" . $shippingCost . "</b><br>\n";
152 $retnb .= "Time left: <b>" . getPrettyTimeFromEbayTime($resp->Item->TimeLeft) . "</b><br> \n";
153 $retnb .= "</div></td> \n";
154 $retnb .= "<td $cellColor><div align=\"left\"> <!-- left align item details --> \n";
155 $retnb .= "Seller ID: <b>" . $resp->Item->Seller->UserID . "</b><br> \n";
156 $retnb .= "Feedback score: <b>" . $resp->Item->Seller->FeedbackScore . "</b><br> \n";
157 $retnb .= "Positive Feedback: <b>" . $resp->Item->Seller->PositiveFeedbackPercent . "</b><br>\n";
158 $retnb .= "</div></td></tr></table> \n<!-- finish table in getSingleItemResults --> \n";
159
160 } else {
161 // If there was no response, print an error
162 $retnb = "Dang! Must not have got the GetSingleItem response!";
163 } // if $resp
164
165 return $retnb;
166
167 } // End of getSingleItemResults function
168
169
170 // Use itemId from selected most watched item as input for a getRelatedCategoryItems call
171 function getRelatedCategoryItemsResults ($selectedItemID) {
172 global $m_endpoint;
173 global $appid;
174 global $responseEncoding;
175
176 // Construct the getRelatedCategoryItems call
177 $apicallc = "$m_endpoint";
178 $apicallc .= "OPERATION-NAME=getRelatedCategoryItems";
179 $apicallc .= "&SERVICE-VERSION=1.0.0";
180 $apicallc .= "&CONSUMER-ID=$appid";
181 $apicallc .= "&RESPONSE-DATA-FORMAT=$responseEncoding";
182 $apicallc .= "&maxResults=3";
183 $apicallc .= "&itemId=$selectedItemID";
184
185 // Load the call and capture the document returned by eBay API
186 $resp = simplexml_load_file($apicallc);
187
188 // Check to see if the response was loaded, else print an error
189 if ($resp) {
190
191 $retnc = '';
192
193 // Verify whether call was successful
194 if ($resp->ack == "Success") {
195
196 // If there were no errors, build a table for the 3 related category items
197 $retnc .= "<!-- start table in getRelatedCategoryItemsResults --> \n";
198 $retnc .= "<table width=\"100%\" cellpadding=\"5\" border=\"0\" bgcolor=\"#FFFFA6\"><tr> \n";
199 $retnc .= "<td colspan=\"3\"><b>eBay shoppers that liked items in the selected ";
200 $retnc .= "item's category also liked items like the following from related categories:</b>";
201 $retnc .= "</td></tr><tr> \n";
202
203 // If the response was loaded, parse it and build links
204 foreach($resp->itemRecommendations->item as $item)
205 {
206 // For each item node, build a link and append it to $retnc
207 $retnc .= "<td valign=\"bottom\"> \n";
208 $retnc .= "<div align=\"center\"> <!-- center align item details --> \n";
209 $retnc .= "<img src=\"$item->imageURL\"> \n";
210 $retnc .= "<p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</a></p> \n";
211 $retnc .= "</div></td> \n";
212 } // foreach
213 $retnc .= "</tr></table> \n<!-- finish table in getRelatedCategoryItemsResults --> \n";
214
215 } else {
216 // If there were errors, print an error
217 $retnc = "The response contains errors<br>";
218 $retnc .= "Call used was: $apicallc";
219 } // if errors
220
221 } else {
222 // If there was no response, print an error
223 $retnc = "Dang! Must not have got the getRelatedCategoryItems response! <br> $apicallc";
224 } // if $resp
225
226 return $retnc;
227
228 } // End of getRelatedCategoryItemsResults function
229
230
231 // Make returned eBay times pretty
232 function getPrettyTimeFromEbayTime($eBayTimeString){
233 // Input is of form 'PT12M25S'
234 $matchAry = array(); // null out array which will be filled
235 $pattern = "#P([0-9]{0,3}D)?T([0-9]?[0-9]H)?([0-9]?[0-9]M)?([0-9]?[0-9]S)#msiU";
236 preg_match($pattern, $eBayTimeString, $matchAry);
237
238 $days = (int) $matchAry[1];
239 $hours = (int) $matchAry[2];
240 $min = (int) $matchAry[3]; // $matchAry[3] is of form 55M - cast to int
241 $sec = (int) $matchAry[4];
242
243 $retnStr = '';
244 if ($days) { $retnStr .= " $days day" . pluralS($days); }
245 if ($hours) { $retnStr .= " $hours hour" . pluralS($hours); }
246 if ($min) { $retnStr .= " $min minute" . pluralS($min); }
247 if ($sec) { $retnStr .= " $sec second" . pluralS($sec); }
248
249 return $retnStr;
250 } // function
251
252 function pluralS($intIn) {
253 // if $intIn > 1 return an 's', else return null string
254 if ($intIn > 1) {
255 return 's';
256 } else {
257 return '';
258 }
259 } // function
260
261 // Display the response data
262 // If button clicked for most watched item, display details and related category items
263 if (isset($_POST['Selection'])) {
264 $selectedItemID = $_POST['Selection'];
265 print getMostWatchedItemsResults($selectedItemID, $cellColor);
266 print getSingleItemResults($selectedItemID);
267 print getRelatedCategoryItemsResults($selectedItemID);
268 } else {
269 // If button not clicked, show only most watched items
270 print getMostWatchedItemsResults('', '');
271 }
272
273?>
274
275</body>
276</html>