Commission Junction Web Services

December 16th, 2009 → 1:56 pm @ Eric Nagel // 6 Comments

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.

Commission JunctionCJ 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:

  • 971450243
  • 971361785
  • 971179939
  • 971144045
  • 971097290
  • 971086202

Yet the SOAP API only returned 3 of them:

  • 971086202
  • 971144045
  • 971450243

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.


Tags: , , ,

6 Comments → “Commission Junction Web Services”


  1. Scott Jangro

    8 months ago

    Good stuff Eric. I’ve been meaning to write this blog post for months now. I’m glad to see that there’s a second person in the world who has looked at their API. ;)

    If my memory serves, they’re moving toward REST and away from SOAP. I’ll see if I can check in with someone there on their plans to support each.


  2. Jason

    5 months ago

    Hey Eric,

    I would be interested to see if changing the “countries” parameter in the soap call to “all” fixed it for you. It seemed to fix it for me. I had the exact same issue, basically I was only getting back 1 result when I should have been getting back 3. I changed it from “US” to “all” and it returned all 3 results for me.

    Let me know your results if you would not mind.

    Cheers,

    Jason


  3. Eric Nagel

    5 months ago

    Hi Jason – interesting idea… and it worked! Nice find!

    However, after I wrote this, Alex from CJ reached out to me and told me to use the REST method. “Some consideration has been put into an eventual migration off SOAP.” Which is fine with me – I like REST better.

    Thanks for the tip!


  4. Jason

    5 months ago

    So, it should be noted that I basically removed all I could from the filtering:

    $results = $client->findPublisherCommissions(array(
    ‘developerKey’ => $developerKey,
    ‘date’ => $date,
    ‘dateType’ => ‘posting’,
    ‘countries’ => ‘all’,
    ));


  5. Jason

    5 months ago

    Until they can give us the date parameter in the REST request, I cannot move off of SOAP. I don’t have a preference for one over the other; except I prefer the stability of curl over the PHP soap client and I like the “ease of use” of the PHP soap client over curl (not that curl is THAT hard — I am specifically talking of having curl talk SOAP; otherwise curl is cake). There is also the HTTPRequest PHP object which I have not played much with: http://usphp.com/manual/en/http.httprequest.php

    Just my 2 cents.

  6. [...] He saw my post on Tracking ShareASale Commissions with Prosper202 and another post on using the Commission Junction Web Services and wanted some info on how to combine the [...]


Leave a Reply