Try our mobile site, m.phish.net!

Phish.net: a Project of the Mockingbird Foundation


Caching with the Phish.net API

Updated by Adam Scheinberg

The Phish.net API is an HTTP/javascript API. Each request means the client asks for a file from phish.net in addition to your own site's HTML, javascripts, stylesheets and images. If you use our hosted callbacks, that's another additional HTTP call.

It's easy to improve upon that speed by caching the results of your API query. Sure, it's another call to your server rather than ours, which one might think is an equal offset, but using this method also improves the speed in the DOM by loading only after the rest of the page has loaded.

You'll want to start by using the tutorial called "Another Way to Use the Phish.net API". Using this method, you're improving the speed of each page load by placing the content into a <div> dynamically, after the HTML has loaded and rendered. However, you've introduced some lag as your server makes the request for the API.

We can offset this by modifying the phishnet-api-helper.php script. The script is configured to ask for the API each time it is called. However, even during a show, rarely is a song less than 5 minutes. So, if we modify the script like such, we should see another small performance gain.

phishnet-api-cached.php <?php

#~~~ Phish.Net API Helper (Caching Version)
#~~~ version 2.0, 2010-09-29
#~~~ written by Adam Scheinberg, adam at phish dot net
#~~~ requires PHP 5
#~~~ if you need this script for PHP < 5, 
#~~~ see http://api.phish.net/phishnet-api-cache-old.php.txt

$cacheDir     './cache/'//you'll need a *writable* directory
$mins        5//how long we cache the file, in minutes

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ READ ME FIRST ~~~~~~~~~

You should not need to modify ANYTHING in this script for it to work.  
On almost all servers that support PHP, you should be able to simply 
upload it and it will work. 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

$cacheFile $cacheDir.'phishnetapi.html'

// if the cache file exists, we'll use it
if(file_exists($cacheFile) && filemtime($cacheFile)<time()-(60*$mins)) {
    include 
$cacheFile
    exit; 
}

$method 'pnet.shows.setlists.latest'

// we'll try to make sure your server allows us to contact phish.net
@ini_set('allow_url_fopen'1);

// this is the line that does the magic
$fgc = @file_get_contents('https://api.phish.net/api.js?api=2.0&method='.$method.'&format=html'); 

echo 
$fgc

// here's the caching part
if(is_writable($cacheDir)) {
    @
file_put_contents($cacheFile$fgc);
}