For no essential reason I wanted to add the Southern Oscillation Index (NINO 3.4) and Indian Ocean Dipole index (updated weekly) to my main weather page (http://www.dcnicholls.com/wx). The data are available as time series on the Australian Bureau of Meteorology site. So the idea was simply to read the data pages, chop out the latest index values, and insert them into my web page. Sounded easy but it wasn't as it turns out to be quite difficult (impossible?) to use something like Javascript to access cross domain pages.
I finished up using PHP to read the data and "echo" it, and the jQuey Ajax routines to call the PHP and accept the output into a form that I could use Javascript to add to the page.
The PHP code is:
Code: Select all
<?php
header('Content-type: text/plain'); //probably unnecessary
$soi34 = file_get_contents('http://www.bom.gov.au/climate/enso/nino_3.4.txt');
if (soi34) { $soix = substr($soi34,-6); } // the data I want is the final 5 characters
else { $soix = "n/a"; }
$iod= file_get_contents('http://www.bom.gov.au/climate/enso/iod_1.txt');
if ($iod) { $iodx = substr($iod,-6); }
else { $iodx = "n/a"; }
echo "$soix,$iodx";
?>
Code: Select all
var bomproxy="/wx/p.php";
var bomdata = $.ajax({url: bomproxy, async: false, dataType: "text" }).responseText;
var rawbomdata=bomdata.split(',');
var soi = rawbomdata[0];
var iod = rawbomdata[1];
There are probably better ways to do this but I'm a neophyte js, Ajax and PHP programmer
The BOM stuff is available via http://www.bom.gov.au/climate/IOD/about_IOD.shtml
DN