Title: Need help starting with MtGox API Post by: SgtSpike on February 12, 2012, 08:57:09 AM Alright, so I am trying to utilize the MtGox API. JSON still confounds me, so that's what I am having trouble with.
If I use the first block of code posted on the wiki page, I end up with this as output: Code: array(8) { ["Login"]=> string(8) "MYLOGIN" ["Index"]=> string(4) "6532" ["Rights"]=> array(2) { [0]=> string(8) "get_info" [1]=> string(5) "trade" } ["Language"]=> string(5) "en_US" ["Created"]=> string(19) "2011-04-29 21:51:06" ["Last_Login"]=> string(19) "2012-02-12 06:23:55" ["Wallets"]=> array(2) { ["BTC"]=> array(5) { ["Balance"]=> array(4) { ["value"]=> string(10) "0.98988000" ["value_int"]=> string(8) "98988000" ["display"]=> string(15) "0.98988000Â BTC" ["currency"]=> string(3) "BTC" } ["Operations"]=> int(137) ["Daily_Withdraw_Limit"]=> array(4) { ["value"]=> string(12) "400.00000000" ["value_int"]=> string(11) "40000000000" ["display"]=> string(17) "400.00000000Â BTC" ["currency"]=> string(3) "BTC" } ["Monthly_Withdraw_Limit"]=> NULL ["Max_Withdraw"]=> array(4) { ["value"]=> string(12) "400.00000000" ["value_int"]=> string(11) "40000000000" ["display"]=> string(17) "400.00000000Â BTC" ["currency"]=> string(3) "BTC" } } ["USD"]=> array(5) { ["Balance"]=> array(4) { ["value"]=> string(7) "0.06833" ["value_int"]=> string(4) "6833" ["display"]=> string(8) "$0.06833" ["currency"]=> string(3) "USD" } ["Operations"]=> int(167) ["Daily_Withdraw_Limit"]=> array(4) { ["value"]=> string(10) "1000.00000" ["value_int"]=> string(9) "100000000" ["display"]=> string(12) "$1,000.00000" ["currency"]=> string(3) "USD" } ["Monthly_Withdraw_Limit"]=> array(4) { ["value"]=> string(11) "10000.00000" ["value_int"]=> string(10) "1000000000" ["display"]=> string(13) "$10,000.00000" ["currency"]=> string(3) "USD" } ["Max_Withdraw"]=> array(4) { ["value"]=> string(10) "1000.00000" ["value_int"]=> string(9) "100000000" ["display"]=> string(12) "$1,000.00000" ["currency"]=> string(3) "USD" } } } ["Trade_Fee"]=> float(0.55) } array(2) { ["usds"]=> string(7) "0.06833" ["btcs"]=> string(7) "0.98988" } But as I understand the json_decode method, it should look something formatted more like this: Code: array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } So, two questions. 1) Why isn't the output pretty/readable? 2) If I wanted to grab a specific variable from that JSON output, what would the syntax look like? Say, if I wanted to store my current BTC balance in a variable, what would the code look like? Title: Re: Need help starting with MtGox API Post by: SgtSpike on February 12, 2012, 09:37:24 PM Bump?
Title: Re: Need help starting with MtGox API Post by: theymos on February 13, 2012, 01:13:07 AM Bitcoin indents its JSON output to make it more readable, but this isn't required. PHP's json_encode doesn't indent. Extra whitespace in JSON is ignored.
Code: array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } This is var_dump output, not JSON. Title: Re: Need help starting with MtGox API Post by: SgtSpike on February 13, 2012, 03:01:46 AM Thanks for the reply.
The code I am using also uses var_dump, so I am wondering why it doesn't output in the same manner. Code: <?php function mtgox_query($path, array $req = array()) { // API settings $key = ''; $secret = ''; // generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems $mt = explode(' ', microtime()); $req['nonce'] = $mt[1].substr($mt[0], 2, 6); // generate the POST data string $post_data = http_build_query($req, '', '&'); // generate the extra headers $headers = array( 'Rest-Key: '.$key, 'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($secret), true)), ); // our curl handle (initialize if required) 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); // run the query $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('Invalid data received, please make sure connection is working and requested API exists'); return $dec; } // example 1: get infos about the account, plus the list of rights we have access to var_dump(mtgox_query('0/info.php')); // old api (get funds) var_dump(mtgox_query('0/getFunds.php')); // trade example // var_dump(mtgox_query('0/buyBTC.php', array('amount' => 1, 'price' => 15))); Ultimately, it doesn't matter what the var_dump looks like, I was just curious why it didn't show up as it does in all the examples I see. Biggest issue is how do I access the data? If I want to grab the wallet balance from that JSON dump, how would I go about doing so? Once I know the syntax to working with the JSON data, I should be golden. Title: Re: Need help starting with MtGox API Post by: theymos on February 13, 2012, 04:56:31 AM After it's decoded it's just a normal PHP variable. In this case it looks like you'd get the balance using $dec["Wallets"]["BTC"]["Balance"]["value"]. (This is a multi-layer array. Each [] goes down a layer.)
Title: Re: Need help starting with MtGox API Post by: SgtSpike on February 13, 2012, 05:20:23 AM After it's decoded it's just a normal PHP variable. In this case it looks like you'd get the balance using $dec["Wallets"]["BTC"]["Balance"]["value"]. (This is a multi-layer array. Each [] goes down a layer.) Easy enough. Guess I just couldn't understand the array structure with the lack of spacing/indentation. Thank you much!EDIT: Just realized that it is properly spaced/indented in the HTML source when viewing the page, so that helps a ton. Just posting that in case it helps someone else. |