You'll need to do your own error checking; this isn't particularly robust.
function mtgox()
{
$mtgox = file_get_contents("https://www.mtgox.com/code/data/ticker.php");
if (!$mtgox) return 0;
$mtgox = json_decode($mtgox);
return $mtgox->ticker->last;
}
not ideal. I'll advice to use volume-based rate. In this script it calculates average volume-based rate for last 3 hours.
<?
$ch=curl_init('https://mtgox.com/code/data/getTrades.php');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_TIMEOUT,15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$headers = array(
// "Host: mtdox.com",
"User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30",
"Accept: */*",
"Accept-Language: en-US;q=0.8,en;q=0.4",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret=curl_exec($ch);
if (curl_errno($ch)!=0) {
echo curl_error($ch);
curl_close($ch);
exit;
}
curl_close($ch);
$o=json_decode($ret,true);
if (!is_array($o)) exit("Invalid data");
$o1=array_reverse($o);
$start=$o1[0]['date'];
$end=$start-3*60*60;
$total_btc=0;
$total_usd=0;
$cnt=0;
for (reset($o1);list(,$v)=each($o1);) {
if ($v['date']<$end) break;
$total_usd=$total_usd + $v['price']*$v['amount'];
$total_btc+=$v['amount'];
$cnt++;
}
if ($total_usd <=0 || $total_btc<=0) exit("Zero volume");
$rate=$total_usd / $total_btc;
echo "Rate is $rate USD/BTC\n";
Also, mtgox has some 'vwap' key in ticker array. I don't know what exact meaning it has, but it is near to the value my script provides. Maybe for different time period.