"Find something you love, and go for it."
DIY Dollars header image 2

Automating Shareasale Affiliate Reporting w/ PHP & cURL

July 23rd, 2008 by shawn · 5 Comments


When you begin to get deep into the world of affiliate marketing, a few things happen:

  1. You begin to miss your family, because you’re always doing “computer stuff” they don’t understand
  2. You have a lot of accounts at different merchants/affiliate networks and you log into those accounts all the time to check your income
  3. You should be making some money

The number 3 part is what keeps people like you and I going. Otherwise we’d have better things to do with our time, right? Right. In fact – even while making money, we have better things to do with our time.

Hmmm…so what can we do about that? How can we maximize our time? I know: let’s do what I’m constantly harping about, and make a computer do the job we don’t need to do. In this case, let’s make a computer log into our affiliate accounts and check stats. Sound good? Good.

For this example, I’m going to show you how to use PHP and cURL to check your Shareasale stats. (Yes, that’s my affiliate link. Come onnn, just click it and join.) I’m using SAS in this example just because I mentioned them recently in the post “Making Money With Shareasale” and I’m soon going to make even more Shareasale tools available to you. So I thought, “Why not prepare everyone with a nice way to automatically gather their stats?”. But the following lesson can be applied to just about any affiliate site…or any website for that matter.

I assume you are familiar with running PHP scripts, at least. Because I sure as hell ain’t gonna teach you how to write a basic PHP script when there are perfectly-good $50 books with animals on the cover to show you that. (And, perfectly good free websites to help you learn PHP, as well.)

So let’s just jump into it. Here’s a script that logs into Shareasale, grabs the current amount of income, and shows it to you.

<?php
$username="superoptimist";
$password="ibelieveinmyself";
function return_between($string, $start, $stop, $type)

    {
    $temp = split_string($string, $start, AFTER, $type);
    return split_string($temp, $stop, BEFORE, $type);
    }
$curl = curl_init();curl_setopt($curl, CURLOPT_REFERER, "http://www.shareasale.com/a-login.cfm"); //Where I came from..

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

curl_setopt($curl, CURLOPT_COOKIEFILE, '/public_html/cookies/cookie.txt');

curl_setopt($curl, CURLOPT_COOKIEJAR, '/public_html/cookies/cookie.txt');

curl_setopt($curl, CURLOPT_POSTFIELDS, "step2=True&username=$username&password=$password&submit.x=40&submit.y=19");

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl, CURLOPT_URL, "http://www.shareasale.com/a-login.cfm");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$page = curl_exec($curl);

echo 'logged in';

curl_setopt($curl, CURLOPT_GET, 1);

curl_setopt($curl, CURLOPT_URL, "http://www.shareasale.com/a-main.cfm");

curl_setopt($curl, CURLOPT_REFERER, "http://shareasale.com/a-login.cfm");

$page = curl_exec($curl);

$total=return_between($page, '<font color=gold> <B>$', '</B>', EXCL);

echo 'got income amount';

echo 'Total Amount $'.$total;

curl_close($curl);

?>

And that’s it. Put your real SAS login info up in there, run it, and you will see a number appear on your screen – like $0.00, if you just opened your Shareasale account. But hopefully, eventually you’ll see some real income.

So why not just log in, manually? Well the point of me sharing this script is not to give you a marginally-useful script that you can use to get your current income. You can do so much more with this script once you think about the possibilities. Here are some:

  • Make an affiliate dashboard, and include all your affiliate reports in a nice, single menu with that day’s sales, that day’s hits, current income, etc.
  • Grab all your affiliates’ daily sales (running daily via cron) and throw that into a database. Use that to generate reports to show you things like: overall most profitable day of the week, most profitable merchant, product, etc. If you run a few scripts like this – say, one for SAS, one for CJ, one for CCBill, etc. and put it all together you can view reports like this across all your merchants and affiliate networks.
  • Grab datafeeds daily or weekly from your merchants, and then update your databases (the ones that run your sites) with the latest products.

Get the idea? This is just an example of basic principles – but cURL is a powerful tool that can almost precisely behave like a web browser – just faster and automatically.

Hopefully you can follow the gist of what this script does, but here are a couple of pointers in case you need some cURL help.

Send what is expected. You must always send GET when the server expects a GET request, and a POST when POST is what’s expected. How to find that stuff out? LiveHTTPHeaders. Possibly the best FireFox plugin out there. At least to me. But there’s probably some weirdo out there with an Fellow Furry Finder plugin or something who thinks that’s the best. (Sorry, that’s not an actual plugin.)

Use LiveHTTPHeaders by selecting it, and then logging into the site you wish to write a cURL script for, and then examining the confusing stuff in the little window. All the stuff you will see is the conversation between your browser and the server. Your goal (in writing an automation script) is to replicate that conversation, or the format of that conversation.

Send in the order that is expected. This means that you must always have the proper referrer with each target URL. If every time a user who goes to accountadmin.php must have come from mainmenu.php, you better put mainmenu.php as your referrer when telling cURL to go to accountadmin.php. Follow the path that you would normally take (as a human, navigating a site).

Respect server resources. Do not do things like hammer a server every five seconds, or visit every hour and downloading a bunch of pics, or anything else like that. Remember Eli’s QUIT tool? Yeah, me too. I miss it. (*Sniff*). See, he shut down the whole damn thing just because of someone out there abusing the tool. If you do things like that to other people, they too will probably shut it down (at least, to your IP). Or they might track you down and punch you in the throat – who knows.

So did we learn something, kids? I hope so. I ain’t here to give ya’ll step-by-step instruction or nuthin’ – but I wanted to at least whet your appetite for some of the things you can do with cURL and maybe give you some ideas. Any questions? I’ll do my best to answer.

Related Posts:



    Tags: Tools




    RSS feed

    5 Comments

    Comment by Polina
    2008-07-29 18:16:31

    Oh, I wish I had something to automate my everyday routine work – it takes a lot of time and when I am finally done with all the tasks, I wish to sleep badly… my family gets angry about me spending my whole life looking the the PC’s display:(

    (Comments wont nest below this level)
     
    Comment by April
    2008-07-30 04:13:23

    Cool idea! I try to automate as much as I can…if only I could get the dishes and clothes to actually make it into their respective washing machines on their own!

    (Comments wont nest below this level)
     
    Comment by Barbara Feldman
    2008-10-04 18:00:47

    I gave this a try, and am getting an undefined function error for “split_string”

    Can you elaborate?

    (Comments wont nest below this level)
     
    Comment by shawn
    2008-10-06 22:47:56

    That function is part of this cURL library which can be found here: http://www.schrenk.com/nostarch/webbots/DSP_download.php

    (Comments wont nest below this level)
     
    Comment by 24hseo.com
    2009-04-15 04:19:16

    Cool idea! I try to automate as much as I can…if only I could get the dishes and clothes to actually make it into their respective washing machines on their own!

    (Comments wont nest below this level)
     

    Sorry, the comment form is closed at this time.