Resources for Developers Using Amazon's Product Advertising API
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· FAQs  
· One-Second Rule  
· PHP Examples  
· Tips for Associates  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with

Disappointed by and the Associates program?

Please consider

Shareasale manages affiliate programs for thousands of merchants. The site makes it easier to apply to programs and create links. And simple-to-use datafeeds are offered by most merchants.

Getting REST Results with Simple Caching

Here is a function with demonstrates how to combine fetching REST results from Amazon's A2S servers with a simple caching scheme using a MySQL database.

function getPage($url, $maxage=7, $cache = true)
  $body = "";
  if ($cache)
     // Get cached result
     $result = mysql_query("SELECT Body, TO_DAYS(NOW()) "
       . "- TO_DAYS(Updated) FROM amazoncache WHERE "
       . "URL = '" . addslashes($url) . "'");
     if (list($body, $age) = mysql_fetch_row($result))
        // If not too old, return cached value
        if ($age <= $maxage)
           return $body;

  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_USERAGENT, 
    'YourName/1.0 (+'); 
  curl_setopt ($ch, CURLOPT_HEADER, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($ch, CURLOPT_TIMEOUT, 120);

  $result = curl_exec ($ch);

  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $result = substr($result, $hsize);

  // On error, try using old cached result
  if ($code >= 400)
     $result = $body;


  // Store returned value in cache
  if ($result != '')
      $xbody = trim(addslashes($result));
      mysql_query("INSERT IGNORE INTO amazoncache (URL, Body, "
    . "Updated) VALUES ('$url', '$xbody', NOW()) "
    . "ON DUPLICATE KEY UPDATE Body='$xbody', "
    . "Updated=NOW()");
  return $result; 

The function accepts three arguments. The first is the URL. The second is the maximum age (in days) results should be cached. The third allows you to bypass the cache by setting it to false. Only the first argument is required, the others are optional. The function returns the results of the query.

The method assumes that the current database contains a table called amazoncache with three fields. You can create the table using the following SQL statement:

CREATE TABLE `amazoncache` (
  `Cache_id` int(10) NOT NULL auto_increment,
  `URL` text NOT NULL,
  `updated` datetime default NULL,
  `body` text,
  PRIMARY KEY  (`Cache_id`),
  UNIQUE KEY `URL` (`URL`(255)),
  KEY `Updated` (`updated`)

The table has a Unique index on the URL which is used as the key for the cached data. An index on the Updated column makes periodic purging of the database quicker. You should periodically (i.e. once a day) purge the amazoncache table with a query like:

  TO_DAYS(Updated) > 90

Copyright © 2020 by Roger Smith