How To Add Affiliate Products From LinkShare

by derek on January 22, 2009 · 79 comments

In this next step of our case study on getting started with affiliate marketing, we will examine how to automate the process of adding affiliate products from LinkShare to our site.

Merchandiser Data Feed Program

LinkShare offers a service that they refer to as the Merchandiser Data Feed Program that allows publishers to maintain a database of product links that is updated daily. Unfortunately, there is a one-time setup fee of $250 unless specific criteria has been satisfied.

Since we are hoping to get started with as little upfront cost as possible, this is probably not our best option and is one that I have not personally explored. However, there is another option for those of us that do not meet the criteria to avoid the setup fee.

Merchandiser Query Tool API

Before you get scared by the mention of API, let me tell you that this is extremely easy to implement and does not require extensive technical knowledge when you use the process that I am about to share with you.

The Merchandiser Query Tool API is a Web service that allows you to run a query against any one, or all, of the merchants that you have a partnership with at LinkShare and will return a listing of affiliate products. The query can return a maximum of 4,020 search results, which should be more than sufficient for our needs.

So how do we leverage this service?

In order to efficiently use this service, we are going to create a custom script (fancy word for PHP page) that we can use throughout our site to display relevant affiliate products.

There are a few prerequisites that must be met in order to use this script: 1) we have an account at LinkShare.com and have been approved for at least one merchant; 2) we have requested and received our web services token from LinkShare.

Creating Custom LinkShare Script

Now that we have satisfied the prerequisites, we are ready to create our script. Don’t worry, it really isn’t as bad as it sounds. To get our script working on our site, we need to perform the following steps.

  • Save the code from Code Example #1 to a file named linkshare.php
  • Open the file in a text editor and insert our LinkShare token on Line 4
  • Upload the file to our web host

Now, wasn’t that easy?

At this point, we have a functioning custom script on our site that will return affiliate products from our merchants at LinkShare. To test that our script is working, simply open a new browser or tab and navigate to the following URL:

1
http://yourdomainname.com/linkshare.php?keyword=yourkeyword

Be sure to actually update the link to include the appropriate domain name and a relevant keyword for at least one merchant. If everything is setup properly, we should see a display of products from at least one of our merchants.

There is the possibility that we will see a message that there aren’t any available products. That doesn’t mean that the script is not working, as we may simply need to change the keyword to match the products of our approved merchants.

Also, this script assumes that our web host supports PHP5 as the call to simplexml_load_file will not work if your host is running PHP4. If you encounter this problem, let me know and I can share the code to work around that issue.

One last note about the code example, you will note that there is a URL on Line 2 that has been commented out. For whatever reason, I found the standard LinkShare link to be extremely unreliable and often resulted in a script failure. By changing to the URL on Line 3, I have not had any problems but feel free to experiment with either URL.

Code Example #1

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
<?
  //$url = "http://feed.linksynergy.com/productsearch";
  $url = "http://65.245.193.50/productsearch";
  $token = "ADDTOKENHERE";  // Add your LinkShare token here
 
  $results = '';
  $resturl = $url."?"."token=".$token;
 
  if (isset($_GET["keyword"]))
  {
  	$keyword = $_GET["keyword"];
  	$resturl .= "&keyword=".$keyword;
  }
 
  if (isset($_GET["cat"]))
  {
  	$category = $_GET["cat"];
  	$resturl .= "&cat=".$category;
  }
 
  if (isset($_GET["max"]))
  {
  	$maxresults = $_GET["max"];
  	$resturl .= "&max=".$maxresults;
  }
 
  if (isset($_GET["mid"]))
  {
  	$mid = $_GET["mid"];
  	$resturl .= "&mid=".$mid;
  }
 
  $SafeQuery = urlencode($resturl);
  $xml = simplexml_load_file($SafeQuery);
 
  if ($xml)
  {
    foreach ($xml as $item) {
        $link  = $item->linkurl;
        $title = $item->productname;
        $imgURL = $item->imageurl;
        $price = $item->price;
        $merchantname = $item->merchantname;
        $description = $item->description->short;
 
        if($link != "")
	        $results .="<div id=\"product\">
	        		<div id=\"product_img\"><a href=\"$link\"><img border=\"0\" src=\"$imgURL\"/></a></div>
	        		<div id=\"product_link\"><a href=\"$link\">$title</a></div>
	        		<div id=\"product_desc\">".$description."</div>
	        		<div id=\"product_price\">Add to Cart: <a href=\"$link\">$price</a></div>
	        		</div>";
	}
  }
 
  if ($results == '') { $results = "<div id=\"product\">There are no available products at this time.</div>"; }
 
  print $results;
?>

Now that we have our custom script installed on the server and everything is working great, we need to investigate how we can actually implement this script on our site.

Implementing Custom LinkShare Script

Ideally, we will want to incorporate one or more affiliate products within the content of a blog post. As an example of what we want to accomplish, look at how the affiliate products are integrated in this post about buying the right football gloves.

In order to implement the script in the manner described here, our site will need to satisfy the following prerequisites: 1) Exec-PHP plugin must be installed, as we will be executing PHP code within a blog post 2) our web host must support cURL (if not, there is a workaround).

Next, we simply need to copy the code from Code Example #2 into our blog post and update the URL parameters as necessary. There are four parameters that we can specify:

  • keyword :: Replace KEYWORD with the keyword that you would like to use for your product search
  • cat :: Replace CATEGORY with a product category to restrict the search results
  • max :: Replace MAX with the maximum number of products to return in the search results
  • mid :: Replace MERCHANT with the merchant id of an advertiser to limit the search results to their products only

Of the four parameters above, only the keyword is required. The remaining parameters are optional, although they will assist in refining our search results so the products match the content of our post as closely as possible.

Note: If you choose not to use one of the optional parameters, remove the entire parameter (i.e. remove “&mid=MERCHANT” not just “MERCHANT” if you choose not to specify a merchant).

To receive the best quality search results, it may be necessary to experiment with a variety of parameter settings.

Code Example #2

1
2
3
4
5
6
7
8
9
<?php
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "http://" . $_SERVER['SERVER_NAME'] . "/linkshare.php?keyword=KEYWORD&cat=CATEGORY&max=MAX&mid=MERCHANT");
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$strPage = curl_exec($cURL);
curl_close($cURL);
echo($strPage);
?>

At this point we should have our custom script installed on the server and have a new blog post that is returning relevant affiliate products within the content of the post.

Wrapping It Up

The process above is exactly what I have done to integrate affiliate products on both Pro Football Workouts and Chicago Football Gifts. Keep in mind that the code provided above does not include any true error handling, but it has been working well on my sites.

Also, as mentioned earlier, you may encounter problems depending on certain configurations made by your web host. The two most likely issues will be support of PHP5 and/or cURL. If you encounter problems with either of these, let me know and I can offer assistance.

Is this a perfect process?

Not by any means, as there is still a good deal of manual work involved and the code could certainly be improved upon (adding robust paging controls, error handling, etc.). This should get you started though and allow you to start adding affiliate products from LinkShare.

Now go earn some money!

Stay In The Loop!

Subscribe to the Derek Semmler dot com feed via RSS or Email to receive notifications when new posts are published. Follow the WordPress ninja on Twitter too!

Previous post:

Next post: