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
- <?php
- $TOKEN="not_here_jack"; // Developer's Token - Change
- $ID="not_here_jack"; // Associate ID - Change
- $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
- $qs = $_SERVER['QUERY_STRING'];
- $fetch = "http://xml-na.amznxslt.com/onca/xml3?t=" . $ID . "&dev-t=" .
$TOKEN . "&" . $qs . "&type=heavy&locale=us&f=" . $XSL ;
- $CacheFileStub = md5(urlencode($qs));
- $CacheFile = "//home/ce/public_html/php/cache/" . $CacheFileStub . ".html";
// Local path to CACHE file
- if((file_exists($CacheFile)) && (($TmpTime=time())-filemtime($CacheFile) <
518400)) // Seven Day Cache Rule -1 Day
- {
- $Data = @ file($CacheFile);
- If ($Data)
- {
- echo $Data;
- }
- }
- else
- {
- $Curl = curl_init();
- curl_setopt($Curl, CURLOPT_URL, $fetch);
- curl_setopt($Curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
- $Delay = 1;
- $Count = 0;
- $Fetched = false;
- $RETRY_COUNT=2;
- do
- {
- $Retry = false;
- $Data = curl_exec($Curl);
- $StatusCode = curl_getinfo($Curl, CURLINFO_HTTP_CODE);
-
- if ($StatusCode == 503)
- {
- if ($Count < $RETRY_COUNT)
- {
- sleep ($Delay);
- $Count++;
- $Delay *= 2;
- $Retry = true;
- }
- }
-
- if ($StatusCode == 200)
- {
- $Fetched = true;
- }
-
- } while ($Retry);
- curl_close($Curl);
-
- if ($Fetched)
- {
- // Good data is in $Data
- $DoCache = true;
- $DoDisplay = true;
- $Output=explode("\n", $Data);
-
- if(strlen($Data) < 200)
- {
- // Blank Page Error
- $DoDisplay = false;
- $DoCache = false;
- }
- if(eregi("xml version", $Output[0]))
- {
- // XSLT Error
- $DoDisplay = false;
- $DoCache = false;
- }
-
- if ($DoDisplay)
- {
- echo $Data;
- }
-
- if($DoCache)
- {
- //Write the good data to cache
- $handle=@ fopen("$CacheFile", "wb");
- @ fwrite( $handle, $Data);
- @ fclose($handle);
- }
- }
- }
- ?>
Latest Source code - PHP/XSLT/Javascript/CSS
Sample Output From Amazon XSLT/PHP Explorer v109
|