Automatically checking affiliate offers for changes in stock or changes in price

Checking stock and price Changes with a script.

A Script To Automate Stock/Price Checking

Update: If you like/use this code please be a jolly nice chap and visit my new web project lastword.at and post there / like on FB / post to twitter. It will make you more attractive to women, 100% Fact.
————————————————–

When I worked in my previous job at at the price comparison website one of the largest parts of the job for the team was “Quality Control” basically going over each of the products in the top 15 or top 30 then checking each individual offer to see if it was still in stock and if the price was correct. It was quite monotonous, not to mention inefficient, I thought at the time that there must be a way to automate this.

If you think, it takes just less than 1 minute to check each offer, with an average of 10 offers per product that would mean over 2 hours to check the top 15 products!
What tended to happen is the quality checks would simply not get done until someone in management noticed the quality was exceptionally poor, at which point the team would be reminded to do more quality checks, which they would do for a week or two until they stopped doing them and we are back at square one.

By using the PHP functions “file_get_contents” and “strpos” we can build an automated system that looks at the affiliate offer, checks to see if it is showing as out of stock, check to see if the price we have is still the current price and return all that info in a matter of seconds. What’s more is the offers are checked every time the page is loaded so the data shown to visitors is always absolutely up to date.

UPDATE
————————————————————————————————————————
As was pointed out in the comments there were a few schoolboy errors with the first version of this script. Namely that it checked the contents of the merchants site every time the page was loaded and for every offer (i.e 1 page request, 3 offers = 3 page requests on the,merchants,site) which could very easily become quite a resource strain on the merchants site which they would certainly have a problem with!

And secondly that the requests from the merchant sites would use your affiliate link which would drive your EPC through the floor, look very suspicious and generally end up with you being kicked from the affiliate program in a very short space of time!

To fix this I have added 2 new features to the script.

Firstly it runs a quick preg_match to search the affiliate link for your affiliate ID and then removes it. This prevents the problem of the merchant receiving a ton of page requests with your affiliate ID attached to it and driving your EPC down / getting you banned from the program.

Secondly the script now caches the results of the script to your server and only rechecks the data from the merchant if the cached version is older than a set amount of time. By default I have set this to 4 hours which means a potential maximum of 6 page requests per day per item. I feel this is not too excessive but also remains fairly upto date from a users perspective.

In order to use this feature you need to create a folder called “cached” at the root level of your domain and chmodd it to “777” in order to be able to write to it.

Finally I also added a nice little bit at the end that tells the user in real words how long ago the price/status was checked :) “Around x seconds/minutes/hours ago”
————————————————————————————————————————
For Example:

My Affiliate Store

Acer Aspire One D255 10.1 inch Netbook £234.25 Out of Stock | Price Change Last Checked 3 hours ago

Hush Hollowfibre Value Range 13.5 Tog King £26.81 In Stock | Price Change Last Checked 3 hours ago

Call of Duty: Black Ops (Xbox 360) £19.99 In Stock | Price Change Last Checked 3 hours ago

——————–
Here we can see the latest data on the price and stock status of the above offers again gathered live from the merchants site.

To implement this on your own site for a single link simply copy and paste the following code into your page, making sure to change the highlighted variables.
————————————————————————————————————————

<?php
//Assign the target url to a the string $check
$check = 'http://www.amazon.co.uk/gp/product/B0039YOZZ0/ref=as_li_ss_tl?ie=UTF8&tag=myaffiliate-21&linkCode=as2&camp=1634&creative=19450&creativeASIN=B0039YOZZ0';
 
//search for any affiliate ID and remove it
//define the affiliate IDs in an array
$affiliate_IDs = array("&tag=myaffiliate-21&linkCode=as2&camp=1634&creative=19450", "112233445566", "this_is_my_affiliate_ID");
 
//and replace them with blanks
$blanked_out   = array("", "", "");
 
$check1 = str_replace($affiliate_IDs, $blanked_out, $check);
 
//Define the cached file name as the url (stripped of none alphanumeric characters) . php
$cached_file = $_SERVER['DOCUMENT_ROOT'] . "cached/" . preg_replace( "/[^\w_-]/", "", strtolower( $check1 ) ) . ".php";
 
// set how long before the cache is considered expired in this case it is 240 minutes (4 hours)
$how_often_to_cache = 240 * 60; 
 
//check to see if the cached file is older than the time limit we set
if (file_exists($cached_file) && 
           (time() - $how_often_to_cache < filemtime($cached_file))) 
        {
 
//if the cached file is not expired then include the contents of it and exit
        	include($cached_file);
}
else {
 
//if the cached file doesn't exist or is expired then run the script to check the stock and price
ob_start(); 
$tally = 0;
$price = 0;
$value1 = '26.81';
$webpage = file_get_contents($check1); 
//check price
          if (strpos($webpage, $value1) !== false) {
$price = 1;          
     }
//check for stock
$find = 'out of stock'; 
          if (strpos($webpage, $find) !== false) {
$tally = 1;          
     }
$find = 'no longer available'; 
          if (strpos($webpage, $find) !== false) {
$tally = 1;          
     }
	 $find = 'not found'; 
          if (strpos($webpage, $find) !== false) {
$tally = 1;          
     }
	 if ($tally > 0){
	 $stock = "<font color='FF0000'>Out of Stock</font>";
	 }
	 else{
		 $stock = "<font color='00BB00'>In Stock</font>";
	 }
 
	 	 if ($price > 0){
	 $price = "<font color='00BB00'>Price is Correct</font>";
	 }
	 else{
		 $price = "<font color='FF0000'>Price Change</font>";
	 }
echo $stock .  ' | ' . $price;
 
//open the cache file (create it if it doesn't exist)
$fp = fopen($cached_file, 'w'); 
 
// save the output of the script to the cached file
fwrite($fp, ob_get_contents()); 
fclose($fp); 
 
// show the results of the script to the user
ob_end_flush(); 
}
$unix_time_passed = time() - filemtime($cached_file);
if ($unix_time_passed < 60)
{
$elapsed = $unix_time_passed . " seconds ago";
}
elseif ($unix_time_passed < 3600)
{
	if (floor($unix_time_passed / 60) > 1){
$elapsed = floor($unix_time_passed / 60) . " minutes ago";
	}
	else {
		$elapsed = "1 minute ago";
	}
}
elseif ($unix_time_passed < 86400){
 
	if (floor($unix_time_passed / 3600) > 1){
$elapsed =  floor($unix_time_passed / 3600) . " hours ago";
	}
	else {
		$elapsed = "1 hour ago";
	}
}
echo "<font size='-2'> Last Checked " . $elapsed . "</font>";
?>

————————————————————————————————————————

 

To incorporate this into your site across the whole site you would simply need to to edit the template of your affiliate store to assign the url to the php value “$check” and the price to the php value “$value1″ then remove lines 3 and 34 from the code above.

You could either paste the code into the template file or save it as a php file and use the php “include” command to include it where needed.

You can use the same code to build a manual link checker (though quite what the point would be?).
Try it out here: Offer Stock/Price Checker