[eluser]johnnytoobad[/eluser]
I found a basic Amazon Product Advertising API (APAA) library here:
http://codeigniter.com/wiki/Amazon_Servi...tegration/
However since last year APAA has required a signature, which has broken this code. I used it as a based, fixed the signature issue, cleaned up the code and introduced the CI pagination class. Since I used this code to get started I thought it was only fair to post my working updated library here in case someone else finds it useful.
My questions here are;
1) I'm only a beginner so if someone more experienced has a second to cast their eye over the code, i would be grateful. Just to make sure i haven't made any outrageous errors
2) Whats the etiquette regarding posting this to the wiki, should I update the old non-working page or create something new?
Here is my code (also attached):
Amazon_api.php (library)
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// -------------------------------------------------------------------------------------------------
/**
* Amazon Product Advertising API library
*
* This is a very basic libary allowing you to search the Amazon Product Advertising API
*
* Based on and inspired by original CI Amazon library by Zac G. (http://zgordon.org) and on code by Richard Cummings (http://richardcummings.info/the-request-must-contain-the-parameter-signature-quick-fix-for-your-php-rest-requests/)
*
* @package CodeIgniter
* @subpackage Libraries
* @author johnnytoobad <http://ellislab.com/forums/member/88806/>
* @version 2.0
* @license http://www.opensource.org/licenses/bsd-license.php BSD licensed.
*
*/
// -------------------------------------------------------------------------------------------------
class Amazon_api
{
function Amazon_api()
{
$this->ci =& get_instance();
}
function search_amazon($keywords, $item_page)
{
//You Amazon API keys
$aws_access_key_id = 'XXXXXXXXXXXXXXXXXXX';
$aws_secret_access_key = 'XXXXXXXXXXXXXXXXXXXXX';
$base_url = 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId;=' . $aws_access_key_id . '&';
//Set parameters for API search, see AWS documentation for a detailed list of parameters
$url_params = array(
'Operation'=>'ItemSearch',
'ItemPage'=>$item_page,
'AssociateTag'=>'YOUR-ASSOCIATE-TAG',
'Version'=>'2006-09-11',
'ResponseGroup'=>'Images,ItemAttributes,EditorialReview',
'SearchIndex'=>'Books',
'Keywords'=>rawurlencode($keywords)
);
//Sort the URL parameters
$url_parts = array();
foreach(array_keys($url_params) as $key)
$url_parts[] = $key."=".$url_params[$key];
sort($url_parts);
//Build and encode the request URL
$url = $base_url . implode('&',$url_parts);
$host = parse_url($base_url . implode('&',$url_parts),PHP_URL_HOST);
$timestamp = gmstrftime('%Y-%m-%dT%H:%M:%S.000Z');
$url = $url. '&Timestamp;=' . $timestamp;
$paramstart = strpos($url,'?');
$workurl = substr($url,$paramstart+1);
$workurl = str_replace(",",",",$workurl);
$workurl = str_replace(":",":",$workurl);
$params = explode("&",$workurl);
sort($params);
$signstr = "GET\n" . $host . "\n/onca/xml\n" . implode("&",$params);
$signstr = base64_encode(hash_hmac('sha256', $signstr, $aws_secret_access_key, true));
$signstr = urlencode($signstr);
$signedurl = $url . "&Signature;=" . $signstr;
$request = $signedurl;
//Send request to AWS
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$parsed_xml = simplexml_load_string($response);
//Return parsed Amazon data
return $parsed_xml;
}
}
?>