How To make a Script to check your Affiliate Landing Page is UP!!!

August 20, 2007

It’s midnight on Tuesday and all of a sudden the offer you were promoting isn’t working anymore!

Have you ever had this happen to you? There’s nothing worse then paying for clicks that aren’t going anywhere. Especially if those are 2-3$ clicks for a niche like mortgage or payday loans. You call your affiliate manager the next day, or even worse don’t notice for a few days, and ask them “hey what happened to xyz campaign?” Do you know what the words out of their mouth are always? “Oh I sent you an email…” but some how it ended up in your spam box or you didn’t pay attention as you get lots of emails from all the affiliate companies every day.

Now what if you had a way to make sure your offers were working and you had control rather then waiting for someone else to alert you? In this example we’re going to make it so that the alert comes to your email but of course this could be adapted to instant messenger, a text message, or any other communication device with a little finessing.

In all these tutorials I’m going to assume you’ve at least coverered the intial tutorials on OOOFF.com on how to do some scraping and parsing of data. If you haven’t please go read the php screen scraping and parsing tutorials.

First step in solving this problem is we need to make a simple script that will go request the affiliate’s landing page you’re promoting. To do that we’ll just use the file_get_contents() function which we know is going to go grab the data from a file or website based on a string we put into the parameters. Then we’re going to assign that to a variable or holder called $page. If you don’t know a $ always indicates a variable. Then lastly we’ll echo the contents that we reach out and grabbed using the echo statement which just echo’s what is in the variable $page to the screen. Here’s what your code should look like.

<?

$page = file_get_contents(“http://www.oooff.com”);
echo $page;

?>

I invite you to make a little text file on your server and called: offer_check.php and putting the lines of code above in it. Of course you’re going to want to replace your destination url with an actual url of your affiliate company. However now comes the bad news. As most offers bounce through a redirect url this simple file_get_contents() function isn’t going to work for most things. However I just wanted to start with it. Now we’re going to a little more robust library and start to use curl. So if we were to do that exact same thing again but this time using curl it would look like this.

<?

$ch = curl_init( “http://www.mylandingpage.com/offer/affid” );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
echo $page;

?>

Most of this is fairly intuitive however there’s a couple of things I’d like touch on. With the Curl library we have the option of setting different things we want to do where the file_get_contents() is only going to request a single page. So if they link goes to Copeac then bounces to the affilliate landing page nothing is going to be returned by file_get_contents() because it will only get the first redirect page. Now we could manually parse and follow each redirect however it’s much simpler to just use Curl who’ll follow the redirects automatically for us if we set the right options.

As you probably guessed the easiest way you set a url in Curl is in the initialization statement. Then we’re assigning that initialized object to the variable $ch. However we haven’t done anything with it yet, this hasn’t requested the page. This just made a container to set some options in. To set those options I’m sure you saw the function curl_setopt() basically it takes arguments of the object/variable holding the contatiner, $ch in this case, to set options on. The second parameter is the option we want to set. We’re setting 2 in this example:

  • CURLOPT_FOLLOWLOCATION – Follows redirects automatically
  • CURLOPT_RETURNTRANSFER – Returns the page output to a variable

So in a nutshell we’re initalizing a container and setting the variable for the url inside that container to the url of our affilaite program. Then we’re telling Curl to automatically follow all redirects to the final destination. Next we say hey don’t just do this but return the page to me so I can do some stuff with it. Curl_exec() pulls does the actual work based on all the options we’ve set in the container and returns that data to the var $page.

Now as long as Curl is enabled on your server or where ever you run your PHP scripts you’re going to get the actual page affilaite page back even if there’s a redirect. I strongly encourage you to take this code and put it on your server and run it. Make sure it works before we go on to the next step.

Now that we have our offer page in the variable $page and we need to find someway to check whether it is up or not. Being that we have the actual source code of the page lets look for something we could check for. For this example we’ll go back to using the homepage of oooff.com. Now direct your browser to oooff.com. After taking a look around I decided the thing most likely to change is the title of the page. So I grabbed the words “PHP Scraping Tutorials” and decided if those didn’t show up then there was something wrong with the landing page. The checking script is going to look like this.

<?

$url = “http://www.mylandingpage.com/offer/affid” ;
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

if(strchr($page,”PHP Scraping Tutorials”)){
echo “still works!”;
}else{
mail(“email@oooff.com”,”Offer is down”,”The offer $url is down.”);
}

?>

The quick and dirty on the second part is it’s saying if this to this, if not this then do something else. What we’re doing is in our if statement we’re using the function strchr() which returns a number for the first position of our string. Guess what happens if there’s no match? That’s right it returns null. In which case the initial if() isn’t true so it moves on to the else. At which time the script emails you to the email you enter, with the subject “Offer is down” and then body “The offer http://www.mylandingpage.com/offer/affid is down.” Now you can manually go check if this is working or not. And if not pause your campaigns until it comes back up or you can talk to an affiliate manager.

The last part of this is to set it to run on a cron or timer automatically. In linux as that’s the OS i use you set a cron using >crontab -e where the -e is for edit. I suggest running this every 10 minutes so you’ll want to enter a line that reads:

*/10 * * * * /home/username/scripts/offer_check.php

Now of course this location is going to be for where ever your script is located on your server. What this will do is run the script and check whether your offers are up ever 10 minutes. And email you if your offers are down.

One quick addition to make this simpler to manage would be to add a second text file to hold the list of offers and matching strings. It would look something like this:

offers.txt

http://www.url1.com/locaiton, text to check
http://www.url2.com/2ndlocation, more text to check

offer_check.php

<?
$lines = file(“offers.txt”);
foreach($lines as $line){

$parts = explode(“,”,$line);

$url = trim($parts[0]);
$search_string = trim($parts[1]);

$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

if(strchr($page,$search_string){
echo “still works!<br />”;
}else{
mail(“email@oooff.com”,”Offer is down”,”The offer $url is down.”);
}

}

?>

(If you take this code and copy it the double quotes do something funny. Make sure you replace them with real double quotes).

Quickly this takes the lines from a file and loads them into an array. After that it loops through each of the lines in the array. Then splits those array’s based on a comma as that’s the format we used for our file. At which time it tests all the conditions just like we did for the single one.

I hope this tutorial helps you save some money in affiliate marketing with offers that have been canceled that you don’t know about. This way you can pause or redirect your google adwords campaigns, CPM buys or where ever else your traffic comes from.

Did you get some good information from this Post? If you did, would you do me a favor and link to it?

newsletter

Want More? The more people listening the more I’ll write.
Subscribe to get business insights in your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.