Amazon Affiliate Link checker – tool 2.0

I’ve been wrestling with the Amazon API and found this great php snippet written by the people over at patchesoft.

Basically they have written a complete class I can call in my scripts to handle all of the communication with the Amazon API for me.

Now I am a complete newbie when it comes to PHP programming, let alone communication with an API.

So I am pretty chuffed to have come up with what I have done so far. Right now the checker tool scans a webpage for any tags with “href” in it, then it assumes those are websites.

That is a pretty safe bet.

Then it checks whether or not “shortened” url’s have been used, as is the case in a lot of affiliate links. It then unwraps the url and digs the associate tag and the product’s ASIN code from the URL.

Afterwards it uses the AmazonAPI-class to make a call to the API with the ASIN in question. It then checks what response it gets for availability when making a call with the ASIN of the product.

If it is an empty response then we just assume that the product is either unavailable, or “available from these sellers”. Both cases will result in my checker showing a “Unavailable” status however..

Things learnt:

-In the Amazon API class there is a field way up reserved for the different types of response groups. These response groups are important to list and details can be found here on what belongs where. At first I didn’t list the response groups that have the availability parameter in it and I was scratching my eyes out why my code wouldn’t work. The path etc was correct. But as I wasn’t including any of the necessary response groups in MY response XML there was no such field as <Availability>.

-PHP security. I have to use my own accessID and scecret key to make calls to Amazon’s Product Advertising API. So a challenge was to think of how to store that data in a safe way. Utilizing varous tricks to limit access to the private side of my website was a new experience!

-In general PHP coding. It seems a lot online is handled through PHP so it’ll be interesting to delve deeper into what kind of information can be passed back and forwards through pages, inputfields and tables.
Especially as I’ll be building another tool I dicussed earlier. For that project I’ll be needing a lot more php to access and store database information!


Things to do:

Right now the crawler just browses the given page and only returns the amazon links.

It would make sense that if you give the main domain of your website, the crawler would start crawling also a few levels deeper so you won’t have to.

So if you for example give your site “” and on your homepage you have a menu, then the crawler would crawl through those menu-links, and return any amazon links that may be there.

This has been suggested by already a few people I have spoken too.

A few issues though is that my script isn’t the fastest around. In fact it’s dead-slow.

So 2 things need to be done:

  1. Speed up the PHP-script so that it cycles through the found links faster
  2. Go as deep as possible when crawling a website and return the URL for the page the crawler is on, to the main table we have all come to know and love <3.

Amazon Affiliate Link checker – tool

It’s a terrible name for a tool. I know..

However it does the job though. Quite often I’ve seen people ask me whenever I’ve sold them a site: “How will I know I changed all the links?”. Because if you leave a tag unchanged on your site, that affiliate sale won’t be going to you..

The mechanics..

For those of you who are not familiar with how affiliate marketing works the above will’ve sounded like hebrew.

Affiliate marketing works in such a way that you push traffic to a site. That site then pays you for the traffic you have sent, should that traffic make a purchase.

I’ll just use amazon as an example as it seems to be the most popular affiliate platform.

So if I send someone through my website to Amazon and should that someone buy something from Amazon, I make a little bit on that sale. That’s how the economics works.

Now your next question should be: How does Amazon know that that person came from your site?

Good question!

Affiliate links

Here we come to the root of our problem. Affiliate links is how amazon keeps track of who came from where. And, more importantly, who to pay what and how much.

Every product mentioned on any niche site pushing people to Amazon uses affiliate links.
These are normal amazon product links, with one difference. These links contain tags that Amazon registers to each individual niche marketer.

So if you buy an already operational niche website that is making money, it’s in your best interest to change all tags in the affiliate links to your own.

However up until now you had to manually sift out the links from the page and just hope you got them all.

With this new tool I made, you can check a URL; It’ll then show you all amazon affiliate links on that site with its anchor text AND it shows who’s tag is attached to it.

So if you see a tag you don’t recognize, simply search for the anchor text of that link and change that link to a link that has YOUR tag.


For the ones who want to know how I did it:

It’s a small php script I have running that parses a certain URL for links:


$html = file_get_contents($url);

//Create a new DOM document
$dom = new DOMDocument;

//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn’t valid XHTML.

//Get all links. You could also use any other tag name here,
//like ‘img’ or ‘table’, to extract other tags.
$links = $dom->getElementsByTagName(‘a’);

//Function to check for shortened URL’s
function unshorten_url($url) {
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce
CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

//make it look nice
echo ‘<table>’;
echo ‘<tr class=”header”>’;
echo ‘<td style=”vertical-align:center; font-weight:bold;”>Anchor text</td>’;
echo ‘<td style=”vertical-align:center; font-weight:bold;”>Amazon tracking code</td>’;
echo ‘</tr>’;

//Iterate over the extracted links and display their URLs
foreach ($links as $link){
echo ‘<tr>’;
if (strpos($link->getAttribute(‘href’), ‘’) !== false || strpos($link->getAttribute(‘href’), ‘’) !== false) {

//Check for non-existant anchor text, most likely an image link
echo ‘<td>’;
echo “IMAGE LINK”, “\n”;
echo ‘</td>’;

echo ‘<td>’;
echo $link->nodeValue, “\n”;
echo ‘</td>’;
$parts = parse_url(unshorten_url($link->getAttribute(‘href’)));
parse_str($parts[‘query’], $query);
echo ‘<td>’;
echo $query[‘tag’];
echo ‘</td>’;
echo ‘</tr>’;
echo ‘</table>’;
echo ‘</br>’;
echo ‘</br>’;
echo ‘<a href=””>Go try another link!</a>’;


Happy link hunting!



edit: Already I’ve seen a few additions I could do to this tool. It could also check if the link is still working. Meaning that is the product still existing in Amazon/check for 404’s…

So many ideas!