Show the Progress of Your Honor System Fund on Your Site

You can let Amazon calculate the progress of a fund drive by scraping the graph that appears on your PayPage.

If your use of the Amazon Honor System is in connection with a drive for a certain amount of money, you show your progress with a graph on your PayPage. To add a graph to a PayPage, go to your list of pages and click “Edit.” Scroll down to section D, “Your Goal Chart and Payment Counter,” and click “Edit.” From there, you can choose your target amount and whether or not to display the graph or payment counter.

If you show the graph on your PayPage, people will know how far along you are in your drive and how far you have to go. Unfortunately it’s available only on your Amazon PayPage, so you can’t indicate the progress as easily on your own site. You can scrape the information to display it locally, though.

The Code

This code requests your PayPage at Amazon and slices out the table with your goal chart. Keep in mind that the regular expressions used here could become obsolete the next time Amazon changes the HTML layout of their PayPages.

<?php
$paypageURL = "insert your PayPage URL";
$payPage = "";

//Get Amazon PayPage based on ID
$contents = fopen($paypageURL,"r");
while (!feof ($contents))
    $payPage .= fgets($contents, 4096);
fclose ($contents);

if (preg_match_all('/<table border=0 cellpadding=1 cellspacing=0 [RETURN]
width=190>.*?Goal Chart.*?<\/table>.*?<\/table>.*?<\/table>/[RETURN]
s',$payPage,$chartTable)) {
    echo $chartTable[0][0];
}

?>

Running the Hack

Put this code in file called get_chart.php and upload it to your server. View it in a browser and you should see your Honor System chart. You could also remove the PHP declarations (<?php and ?>) and drop this code into any existing PHP page.

Hacking the Hack

Keep in mind that get_chart.php contacts Amazon’s servers each time the page is loaded, so it could slow down your page a bit. Instead of requesting the goal chart every time, you could cache the response on your server and refresh the cached version at a certain interval. Here’s the same bit of code with a simple file-caching system.

<?php
$paypageURL = "insert your PayPage URL";
$payPage = "";
$cachefile   = "./chart_table.txt";

// Create the cache file if it doesn't exist
if (!file_exists($cachefile)) {
    $fp=fopen("$cachefile", "w");
    $filetime = (time() - 864000);
} else {
// Or set the last modified time of the cache file
    $filetime = filemtime($cachefile);
}

// Check the last-modified time on the cache file;
// If it's less than 24 hours (86400 seconds), 
// request a new copy and write to the cache file.
if ($filetime < (time() - 86400)) {
    // Get Amazon PayPage based on ID
    $contents = fopen($paypageURL,"r");
    while (!feof ($contents))
        $payPage .= fgets($contents, 4096);
    fclose ($contents);

    if (preg_match_all('/<table border=0 cellpadding=1 cellspacing=0 [RETURN] 
width=190>.*?Goal Chart.*?</table>.*?</table>.*?</table>[RETURN]
/s',$payPage,$chartTable)) {
        $fp=fopen("$cachefile", "w");
        fwrite($fp, $chartTable[0][0]);
        fclose($fp);
    }
}

$fp=fopen("$cachefile", "r");
echo fread ($fp, filesize($cachefile));
?>

This script stores the chart HTML in a file called chart_table.txt and checks that file’s last-modified time. If the file hasn’t been modified for 24 hours, it requests the chart HTML and writes it to chart_table.txt. Finally, it writes out the contents of the file. If your fund drive is a huge success and you’d like to update the file more frequently, you can adjust the update time. PHP measures time in seconds, and 86,400 is the number of seconds in 24 hours. You can add more or less time accordingly.

Get Amazon Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.