PHP CJ API Demo

A small script I wrote for testing the REST API over at Commission Junction.

CJ API Demo

PHP code:
<?php
/*	
 *	Copyright (C) 2011 Dag Jonny Nedrelid
 *	
 *	GENERAL INFORMATION
 *	-------------------
 *	This script retrieves niche products from several different CJ 
 *	advertisers and prints various properties to demonstrate a way 
 *	to connect and parse data from Commission Junction.
 *
 *	RESOURCE
 *	--------
 *	Product Catalog Search Service (REST).
 *	URI: http://help.cj.com/en/web_services/product_catalog_search_service_rest.htm
 */

function ConnectToCJ() 
{
	global $sort_order, $sort_by;
	
	//
	// Build REST URI for product search. Refer to 
	// documentation for more request parameters.
	//
	$URI = 'https://product-search.api.cj.com/v2/product-search?'.
		'website-id=<INSERT ID>'.	// USE YOUR OWN.
		'&low-price=1'.
		'&records-per-page=25'.
		'&sort-order='.	$sort_order .
		'&sort-by='. $sort_by .
		'&keywords='. rawurlencode($_GET['keywords']);
	
	$context = stream_context_create(
	array(
	'http' => array(
		'method' => 'GET',
		'header' => 'Authorization: ' . // USE YOUR OWN.
			'0012345b5ffdb74cd401e1aade0f69cadca29c834781c0936'.
			'b0b0836383b4e3e8dd7b406612347c3813bda24f8354dd649'.
			'6679031d8bc46f0dea1943a747ae0025/0093500ab1417918'.
			'f621234038b1234e8c4b0b22ea9f9cbc1db37a592247676ae'.
			'c528388bad7a06c9532c46fba2d0815e81e1234a9b25d9173'.
			'2f46f93123444dc1'
		)
	));
	
	$response = new SimpleXMLElement(file_get_contents($URI, false, $context));
	return $response;
}

// A separate function to parse data.
function FormatCJResponse($data) 
{
	global $sort_order;
	
	$ProductList = '<table border="0" cellspacing="2" cellpadding="2" style="width:800px; margin: auto 0px">'.
			'<tr style="font-weight:bold">'.
			'<td><a href="?keywords='. rawurlencode($_GET['keywords']) .'&sort-by=name&sort-order='. SetOrder('name') .'>Product Name</a></td>'.
			'<td>Advertiser Name</td>'.
			'<td><a href="?keywords='. rawurlencode($_GET['keywords']) .'&sort-by=price&sort-order='. SetOrder('price') .'>Price</a></td>'.
			'<td>Description</td>'.
			'<td>Image</td>'.
			'</tr>';
	
	$attributes = $data->products->attributes();
	if ($attributes->{'total-matched'} == '0')
		$ProductList .= '<tr><td colspan="5">No products found ...</td></tr>';
	
	foreach ($data->products[0] as $product) 
	{
		// Sanitize data.
		$price = number_format((float)$product->price, 2, '.', ' ');
		
		$image = '<img src="ImageHandler.php?fileName='. 
				$product->{'image-url'} .'">';
		
		// Add to list.
		$ProductList .= '<tr><td colspan="5"> </td></tr>'.
		'<tr>'.
		'<td valign="top">'. $product->name .'</td>'.
		'<td valign="top">'. $product->{'advertiser-name'} .'</td>'.
		'<td valign="top">'. $price .' '. $product->currency .'</td>'.
		'<td valign="top">'. $product->description .'</td>'.
		'<td valign="top">'. $image .'</td>'.
		'</tr>';
	}
	
	$ProductList .= '</table>';
	return $ProductList;
}

// Sorts out individual sorting.
function SetOrder($param)
{
	global $sort_by, $sort_order;
	
	if ($sort_by == $param && $sort_order == 'asc') {
		$order = 'desc" title="Sort by descending order"';
	} else {
		$order = 'asc" title="Sort by ascending order"';
	}
	
	return $order;
}

if (isset($_GET['keywords'])) {
	if ($_GET['keywords'] != '') {
	
		// Sort order.
		if (!isset($_GET['sort-order'])) {
			$sort_order = 'asc';
		} else {
			switch ($_GET['sort-order']) {
			case 'asc':
				$sort_order = 'asc';
				break;
			case 'desc':
				$sort_order = 'desc';
				break;
			default:
				$sort_order = 'asc';
			}
		}
		
		// Sort by
		if (!isset($_GET['sort-by']))
			$sort_by = 'name';
		else
			$sort_by = $_GET['sort-by'];
		
		// Connect to CJ and get formatted results.
		$ProductList = FormatCJResponse(ConnectToCJ());
	}
}
?>

Just check for and print the content in $ProductList:
<form action="" method="get">
<input type="text" name="keywords"> 
<input type="submit" value="Find it on Commission Junction!">
</form><br>

<div id="product-list">
<?php echo isset($ProductList) ? $ProductList : ''; ?>
</div><br>

This document was last updated January 19th, 2012.
Written by: Dag Jonny Nedrelid
©2007-2012 http://thronic.com


Feel free to leave a comment.
Name:
URL:
0




Comment submitted 2012-01-19 13:12:32 CET/CEST
Name: Dag Jonny Nedrelid

@Kevin - Thanks for your comment. I have updated the article with a note and example to check the result variable @ProductList. I hope it helps.



Comment submitted 2012-01-18 21:45:55 CET/CEST
Name: Kevin

I must be a moron; can't get anything to return. Pointed <form action=""> to my equivalent of your http://thronic.com/articles/PHP CJ API Demo.php page, however after keywords are submitted, the expected result is not what I get! (page goes white)