www.enkognedo.com

  • Increase font size
  • Default font size
  • Decrease font size
Home programming PHP Commission Junction Web Services - Using REST

Commission Junction Web Services - Using REST

E-mail Print PDF
User Rating: / 3
PoorBest 

OK.  If you are one to know how to work with XML elements and render their output, you probably will not benefit from this article.  Commission Junction, much like all the rest, i.e. LinkShare, etc... have some type or another Web Service.  Commission Junction still uses SOAP, which I got to work, but did not try to get my output to format nicely.  Instead, I chose to work with REST using cURL to do the work.


The forums over at Commission Junction were somewhat helpful, as I was able to construct a two-part API.  I use WordPress for a lot of my sites and was able to accomplish something similar with much help of the Internet using LinkShare's API.  Here is the two file API I came up with using parts of examples in the forums at CJ and parts of my LinkShare API I already created:

(Name this file as you wish, i.e. cj_api_call.php)

<?php
// Commission Junction Product Catalog Search Service (REST) API
$keyword_string = "playstation3 call of duty";
$keyword_string = urlencode($keyword_string); // handles spaces and such
$count = "2";
// send request to API
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, 'http://www.your_url.com/your_api.php?keyword='.$keyword_string.'&amp;max='.$count);
curl_setopt($cURL, CURLOPT_HEADER, 0); // we do not want the header unless troubleshooting
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$strPage = curl_exec($cURL);
curl_close($cURL);
echo($strPage);
?>

Next, create the actual API.  We might choose to name it something like cj_api.php (this is the file name that will replace "your_api.php" in the previous code:


<?php
$websiteid= "your CJ site numeric id here"; 
// register for your developer's key here:  http://webservices.cj.com/ (input dev key below)
$CJ_DevKey= "your dev key here";
$currency="USD";
$advs="joined"; // results from (joined), (CIDs), (Empty String), (notjoined)
// begin building the URL and GETting variables passed
$targeturl="https://product-search.api.cj.com/v2/product-search?";
if (isset($_GET["keyword"]))
{
$keywords = $_GET["keyword"];
$keywords = urlencode($keywords);
$targeturl.="&amp;keywords=$keywords";
}
 
if (isset($_GET["max"]))
{
$maxresults = $_GET["max"];
$targeturl.="&amp;records-per-page=".$maxresults;
}
 
$targeturl.="&amp;website-id=$websiteid";
$targeturl.="&amp;advertiser-ids=$advs";
$targeturl.="&amp;currency=$currency";
// end building targeturl
 
$ch = curl_init($targeturl);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_DevKey)); // send development key
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$xml = new SimpleXMLElement($response);
curl_close($ch);
 
if ($xml)
{
foreach ($xml->products->product as $item) {
$link  = $item->xpath('buy-url');
$link  = (string)$link[0];
 
$title = $item->xpath('name');
$title = (string)$title[0];
 
$imgURL = $item->xpath('image-url');
$imgURL = (string)$imgURL[0];
 
$price = $item->xpath('price');
$price = '<br />$'.number_format($price[0],2,'.',',');
 
$merchantname = $item->xpath('advertiser-name');
$merchantname = (string)$merchantname[0];
 
$description = $item->xpath('description');
$description = (string)$description[0];
 
 
if($link != "")
$results .="<div id=\"product\">
<div id=\"product_img\"><a href=\"$link\" target=\"_blank\"><img src=\"$imgURL\"/></a></div>
<div id=\"product_link\"><a href=\"$link\" target=\"_blank\">$title</a></div>
<div id=\"product_desc\">".$description."</div>
<div id=\"product_price\"><a href=\"$link\" target=\"_blank\">".$price."</a></div>
</div><br /><br />";
 
}
}
 
if ($results == '') { $results = "<div id=\"product\">There are no available products at this time or no search parameters were specified.  Please try again later.</div>"; }
 
print $results;
?>

Obviously, there are a number of different parameters that can be changed and/or added.  Use CSS to style the output as desired.  If you are using WorkPress, you will need a plugin like "Exec-PHP" to embed PHP into your posts.  After adding and enabling the Exec-PHP plugin, simply insert the first block of code into the HTML side of your post, publish it and it is ready to go!

Let me know if there are any questions.  Constructive feedback is appreciated!






Last Updated on Saturday, 03 April 2010 14:58  

Design Center Tips

Adobe Design Center RSS Feed
Learn how to use and integrate Adobe products with step-by-step tutorials on new features, key workflows, and advanced techniques.

Inside Gaming

  • I recently received the Mass Effect 2 game for Xbox 360 as a gift!  I've got to say, this is one hell of a sequel game to Mass Effect!  The storyline is as intense and involved as ever and the graphics and sound are unsurpassed!

    If I could complain at all, I'd complain about the time it takes to load a game.  Only take about a second to save one, but loading is more like 30 seconds - maybe more.  So, needless to say, moving around from the main screen to in-game can be quite time cumbersome and annoying.

    Read more...
  • Flashpoint just didn't get the high marks it deserved.  For those who like local, campaign play, this is the game for you.  It is intense, very difficult and will keep you on the edge of your seat figuring and carrying out that perfect strategy.

    There is a lot of evasive or eluding type play - meaning that you don't always have to engage the enemy just because you can see them.  If you want to survive in this game, you have to be tactically sound in your decisions, well disciplined, and patient.

    Read more...
  • The graphics were great, the individual scenes awesome, the length of play...   HIGHLY disappointing!  

    I played this game for just over 11 hours.  I don't consider myself a "gamer".  I've always thought of gamers as those who buy every new game as it becomes available to buy and play it straight through to the finish.  Some of these people even use the system to quickly play it, take it back or trade it in for another, or whatever.

    Read more...
  • Never go into battle alone! EA's Montreal development studio is changing the face of how people will play games. Delivering a groundbreaking strategic 3rd person co-op shooter unparalleled in the action genre, EA Montreal's ARMY OF TWO focuses on gameplay centered around TWO man missions, TWO man strategies, TWO man tactics and a TWO man advantage

    Fight along side your team-mate to become the deadliest two-man military outfit a government can buy. The breakthrough gameplay in ARMY OF TWO, with a live or adaptive AI partner, enables you to take advantage of two-man strategies like customizable weapons, vehicles, and tactical shooting.

    From TWO man sniping to parachuting, players will experience action-packed scenarios where they will have to use their wit, strength and a deadly arsenal of shared customizable weapons to successfully defeat their enemies and complete the mission at hand.

    Gamers will fight their way through war, turmoil and a conspiracy so vast it threatens the entire world. When one man is not enough, it's going to take an army of two to save us.

    Key Features

  • "Two Man" Gameplay-Co-op is no longer a mode, it's the game. Team up with a live or AI partner on two-man missions using two-man tactics to ultimately create an advantage that no enemy can withstand.
  • Partner AI-ARMY OF TWO delivers intelligent partner AI (PAI) that interacts, adapts, collaborates and learns from their human partner. The PAI will also react and adjust to the gamer's ability and how they play the game by taking the lead or following, driving strategy or taking orders. It is the dream of the human machine.
  • Customizable Weapons-Customize and upgrade dozens of deadly weapons and share them with your partner off or online.
  • Dynamic
Read more...
  • You are Commander Shepard of the starship Normandy and you are the last hope for all life in the Galaxy. Saren, a rogue member of the elite and untouchable Spectre agents, has discovered the secret to unleashing an apocalyptic force on the galaxy, and only you stand in his way.

    To save the lives of billions, you must do whatever it takes to stop Saren and prevent the return of an ancient force bent on the destruction of all organic life.

    • Experience a rich and engrossing story where your choices decide the fate of galaxy
    • Engage in emotionally charged interactions with the most realistic Digital actors ever seen in a Video game, using an innovative new dialog system
    • Immerse yourself in an incredible new adventure from world-famous masters of the RPG: BioWare
    • Dominate the battlefield with new individual squad member command system-Optimized controls designed specifically for the PC
    • High Resolution visuals - highly detailed textures, characters and environments
    • Fully customizable controls - you can re-map the control scheme any way you like
    • Run & Gun Control - You can assign biotic powers or skills to 'hot keys' allowing you to play Mass Effect with a heavier Focus on action
    • Decryption mini-game
    • New inventory screen GUI and functionality - The enhanced inventory system makes it easier for you to equip and modify your weapons and armor
    Read more...
  • Sponsored Links

    Advertisement