I have been, or can be if you click on a link and make a purchase, compensated via a cash payment, gift, or something else of value for writing this post. Regardless, I only recommend products or services I use personally and believe will be good for my readers.
If you don’t have an account, you first need to signup with LinkShare. Then login, and click on Web Services under Links. Next, generate or update your token. You’ll need this when making requests.
When I first generated my token, the services were not working for me. After contacting support (which was easy to do), they promptly replied:
However, 30 minutes after being generated, the token was active.Please be note that the feed token MAY take up to 24 hours to fully activate from the time it was initially generated/reset.
Building the query URL is fairly straight-forward. At its simplest, you pass your token and a keyword:
http://productsearch.linksynergy.com/productsearch?token=[your token here]&keyword=backpack
Refer to the Merchandiser Query API page for documentation on the other parameters you may use, such as category, merchant, and sorting.
Putting it all together:
$cURL = 'http://productsearch.linksynergy.com/productsearch?token=[your token here]&keyword=backpack'; $xml = simplexml_load_file($cURL); for ($i = 0; $i < count($xml->item); $i++) { $oProduct = $xml->item[$i]; ?> <div class="post"> <h2 class="title"><a href="<?= $oProduct->linkurl ?>" target="_blank" style="text-decoration:none"><?= $oProduct->productname ?></a></h2> <div class="entry"> <p><a href="<?= $oProduct->linkurl ?>" target="_blank"><img src="<?= $oProduct->imageurl ?>" alt="<?= $oProduct->productname ?>" title="<?= $oProduct->productname ?>" style="max-height:100px; max-width:100px; float:left" onerror="ImgError(this)" /></a><?= $oProduct->description->short ?></p> <br style="clear:all" /> </div> <div class="meta"> <p class="byline"><a href="<?= $oProduct->linkurl ?>">$<?= number_format($oProduct->price, 2) ?></a></p> <p class="links"><a href="<?= $oProduct->linkurl ?>" class="more">Get More Info</a> <b>|</b> From <a href="<?= $oProduct->linkurl ?>" class="comments"><?= $oProduct->merchantname ?></a></p> </div> </div> <?php }
Refer to the 3rd step in my Building a Datafeed Site
series for an explanation of the ImgError JavaScript event.
This PHP code is meant to be dropped into a static WordPress template, many of which can be downloaded from free CSS templates.
If you wanted to improve performance, you could save the Merchandiser results to MySQL for local caching.
To see a site built on the LinkShare Merchandiser API, check out this site on Transcend SD Cards.
May 11, 2011 Update
LinkShare has updated the URLs for their APIs. The code here has been reflected with the new URLs.