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.'&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.="&keywords=$keywords"; } if (isset($_GET["max"])) { $maxresults = $_GET["max"]; $targeturl.="&records-per-page=".$maxresults; } $targeturl.="&website-id=$websiteid"; $targeturl.="&advertiser-ids=$advs"; $targeturl.="&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!
| Next > |
|---|







