Eric Nagel

Commission Junction Web Services

I have been, or can be if you click on a link and make a purchase, compensated via a cash payment, gift, or something else of value for writing this post. Regardless, I only recommend products or services I use personally and believe will be good for my readers.

Next in my series of affiliate network API programming examples, I’ve decided to tackle Commission Junction. The goal was to get a list of my stats from yesterday for one website, and store them in my custom tracking script.

CJ has two choices when it comes to how to interface with them: REST and SOAP. Since I already had some SOAP code laying around, I decided to go that route (using the Daily Publisher Commission Service) . However, after I had my script done (took about 20 minutes), I realized CJ was only giving back half of my sales! From their website, I saw 6 sales for this one website:

Yet the SOAP API only returned 3 of them:

So I emailed CJ about this (have yet to hear back from them) but being the impatient person that I am, I checked out the REST API. To my surprise, there is no equivalent to their SOAP Daily Publisher Commission Service – the closest I saw was their Commission Detail Service (REST). However, this service doesn’t take in any date parameters – your choice is yesterday or nothing. For this project, that’s OK as that’s what I want.

So the first thing to do is build the query. Starting with the base URL, add on the parameters you’d like. In my case, it’s the date-type and website-ids.

$cURL = 'https://commission-detail.api.cj.com/v3/commissions?';
$cURL .= 'date-type=event&';
$cURL .= 'website-ids=' . $nWebSiteID;

Then, using curl, grab the results:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cURL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			  'Authorization: ' . $cDevKey,
			  'User-Agent: "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB6 (.NET CLR 3.5.30729)"'
			));

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$cHTML = curl_exec($ch);
if (curl_error($ch)) {
	echo "Curl error: " . curl_error($ch);
} // ends if (curl_error($ch))
else {
	$cXML = simplexml_load_string($cHTML);
	// var_dump($cXML);

	for ($i = 0; $i < count($cXML->commissions->commission); $i++) {
		addCJCommission($cXML->commissions->commission[$i]);
	} // ends for ($i = 0; $i < count($cXML->commissions->commission); $i++)
} // ends else from if (curl_error($ch))

What I have in the loop is a function, addCJCommission(), which takes the commission object as a parameter. This function will add the commission to my database. I’m not going to get into details about that (as it’s specific to my project, and not what you’re doing) but one thing I found out is that objects with a dash in the property name require some special coding. For example, getting the date from the event-date property:

Don’t do this:

$dDate = date("Y-m-d", strtotime($oCJCommission->event-date));

Do this:

$dDate = date("Y-m-d", strtotime($oCJCommission->{'event-date'}));

To get started with Commission Junction Web Services, visit http://help.cj.com/en/web_services/web_services.htm. Thanks to forums.digitalpoint.com for the discussion on setting the Authorization header with curl.