Show statistics FlightMemory on your site
Flightmemory.com is an excellent site for save and display all the statistics about our flights, the duration, the flight number, Company, the type of aircraft, the market, allows you to make judgments about airports and airlines.
Also, automatically generates a map where you can see all the routes flown.
I wanted to show some of my statistics taken from my profile on FlightMemory but to my homepage, Unfortunately, to date, FlightMemory NOT yet have API to extract data.
To overcome this,, was enough to change it script I had already created for RunKeeper and Sportstracklive, and extract the data that I wanted to, or:
- IL Total time spent in flight
- The calculation of the total kilometri volati
- Average duration a flight
Here is the result, visible on my homepage:
We're going to see a piece of the script:
<?
/* Flightmemory.com data parser. It automatically shows data from the stats page for a given username. Set your username, cache file name, and expiration time of the cachefile.
(C) flapane.com - Feb-2013*/
define('FLIGHTMEMORY_USER','type_your_username'); //Flightmemory Username
$expiretime=3; // cache expire time in days
$cachename="flightmemory.cache"; //name of the cachefile
//create the cachefile if it doesn't exist
if (!file_exists($cacheName)) {
$create = fopen($cacheName, 'W');
chmod ("$cacheName", 0644); //set chmod 644
fclose($create);
}
// Is the file older than $expiretime, or the file is new/empty?
$FileAge = time() - filectime($cacheName); // Calculate file age in seconds
if ($FileAge > ($expiretime * 3600 * 24) || 0 == filesize($cacheName))
{
include_once('simple_html_dom.php');
$opts = array(
'http'=>array(
'user_agent'=>'Mozilla/5.0 (Windows; You; Windows NT 5.1; in; rv:1.9.2.3) Gecko/20100401 MRA 5.6 (build 03278) Firefox/3.6.3 (.NET CLR 3.5.30729)',
'Accept: text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8'
)
);
$context = stream_context_create($opts); //create an useragent for simple_html_dom
$html = file_get_html('http://my.flightmemory.com/'.FLIGHTMEMORY_USER, 0, $context);
$chilometri_table = $html->find('table', 2); // chilometri totali
$ore_table = $html->find('table', 3); // ore totali
$statsvoli_table = $html->find('table', 5); //longest-shortest-avg flight
$chilometri = strip_tags($chilometri_table->find(. 'List', 1)->find('td', 1)); //strip tags
$ore = preg_replace("/(?:\S| )+/", "", strip_tags($ore_table->find(. 'List', 0)->find('td', 1)), -1); //strip tags and delete white spaces from hours
$statsvoli = strip_tags($statsvoli_table->find('tr', 3)->find('td', 1)); //strip tags
// Now refresh the cachefile with newer content
$stats = 'Distanza totale:<b> '. $ Kilometers.' Km</b><br />Total time:<b> '.$ore.'h</b><br />Average flight:<b> '. $ Statsvoli.'</b>';
$handle = fopen($cacheName, 'wb');
fwrite($handle, $stats);
}
//cachefile is fresh enough... outputting data
$data=file_get_contents($cacheName);
echo $data;
?>
Once you download the zip file linked to the end of the article, caricatelo its a ftp server.
The only things that we are going to change type_yourusername with your username Flightmemory.com, and the value expiretime (in days) after which the cache file with the statistics will be updated.
Flipping the script, will create a cache file, called flightmemory.cache, that can be positioned where you want.
By way of example, here is a possible content of the cache file:
Total distance:<b> 56,040km</b><br />Time spent in flight:<b> 78:07h</b><br />Average flight:<b> 2,949 km, 4:07 h</b>
The script (complete simple_html_dom) can be downloaded at this address: Flightmemory PHP Parser
Any feedback or advice is welcome!