function mtgox_query($path, array $req = array()) {
$key = 'xxx'; //from your mt. gox account
$secret = 'xxxx'; //from your mt. gox account
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
$post_data = http_build_query($req, '', '&');
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($secret), true)),
);
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, 'https://mtgox.com/api/'.$path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('mtgox_query');
return $dec;
}
//call the method from (https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1) using the function.
$mt_gox = mtgox_query('1/BTCUSD/ticker');
//out put the value.
echo $mt_gox['return']['last']['value'];
//this will dump all the values being returned from this call.
echo '<pre>' . print_r($mt_gox,true) . '</pre>';