Using Amazon Web Services With PHP And XSLT

PHP and Amazon XSLT, Oh my! The benefits of using a server side scripting language and Amazon XSLT may not be apparent at first glance but lets look at them:

  • Very little server firepower is required. The Amazon XSLT processor provides the processing horsepower. Why use anything but XSLT to process XML?
  • Server side scripting allows server side caching  of Amazon product data. This lowers bandwidth and server firepower on high traffic sites. It innoculates against AWS service brownouts.
  • Server side scripting allows Amazon product data to be indexed by search engines. This is a disadvantage to Amazon XSLT users. Call it search engine flypaper.
  • Server side scripting allows error control and logging. Without usage statistics you are lost as an Amazon associate.

Simple PHP fetch/echo script

  1. <?php
  2. $TOKEN="not_here_jack"; // Developer's Token - Change
  3. $ID="not_here_jack"; // Associate ID - Change
  4. $XSL="http://www.celtic-one-design.com/amazon-services/xslt-109.xsl"; // Local path to XSL file - You must download this file, modify it to include your ID, token and AWSversion (to point to the PHP file), then upload it to your host
  5. $qs = $_SERVER['QUERY_STRING'];
  6. $fetch = "http://xml-na.amznxslt.com/onca/xml3?t=" . $ID . "&dev-t=" . $TOKEN . "&" . $qs . "&type=heavy&locale=us&f=" . $XSL ;
  7. $CacheFileStub = md5(urlencode($qs));
  8. $CacheFile = "//home/ce/public_html/php/cache/" . $CacheFileStub . ".html"; // Local path to CACHE file
  9. if((file_exists($CacheFile)) && (($TmpTime=time())-filemtime($CacheFile) < 518400)) // Seven Day Cache Rule -1 Day
  10. {
  11. $Data = @ file($CacheFile);
  12. If ($Data)
  13. {
  14. echo $Data;
  15. }
  16. }
  17. else
  18. {
  19. $Curl = curl_init();
  20. curl_setopt($Curl, CURLOPT_URL, $fetch);
  21. curl_setopt($Curl, CURLOPT_TIMEOUT, 10);
  22. curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
  23. $Delay = 1;
  24. $Count = 0;
  25. $Fetched = false;
  26. $RETRY_COUNT=2;
  27. do
  28. {
  29. $Retry = false;
  30. $Data = curl_exec($Curl);
  31. $StatusCode = curl_getinfo($Curl, CURLINFO_HTTP_CODE);
  32.  
  33. if ($StatusCode == 503)
  34. {
  35. if ($Count < $RETRY_COUNT)
  36. {
  37. sleep ($Delay);
  38. $Count++;
  39. $Delay *= 2;
  40. $Retry = true;
  41. }
  42. }
  43.  
  44. if ($StatusCode == 200)
  45. {
  46. $Fetched = true;
  47. }
  48.  
  49. } while ($Retry);
  50. curl_close($Curl);
  51.  
  52. if ($Fetched)
  53. {
  54. // Good data is in $Data
  55. $DoCache = true;
  56. $DoDisplay = true;
  57. $Output=explode("\n", $Data);
  58.  
  59. if(strlen($Data) < 200)
  60. {
  61. // Blank Page Error
  62. $DoDisplay = false;
  63. $DoCache = false;
  64. }
  65. if(eregi("xml version", $Output[0]))
  66. {
  67. // XSLT Error
  68. $DoDisplay = false;
  69. $DoCache = false;
  70. }
  71.  
  72. if ($DoDisplay)
  73. {
  74. echo $Data;
  75. }
  76.  
  77. if($DoCache)
  78. {
  79. //Write the good data to cache
  80. $handle=@ fopen("$CacheFile", "wb");
  81. @ fwrite( $handle, $Data);
  82. @ fclose($handle);
  83. }
  84. }
  85. }
  86. ?>

Latest Source code - PHP/XSLT/Javascript/CSS

Sample Output From Amazon XSLT/PHP Explorer v109