Bitcoin Forum
September 30, 2023, 08:53:44 AM *
News: Latest Bitcoin Core release: 25.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need help starting with MtGox API  (Read 1789 times)
SgtSpike (OP)
Legendary
*
Offline Offline

Activity: 1386
Merit: 1005



View Profile
February 12, 2012, 08:57:09 AM
 #1

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?
1696064024
Hero Member
*
Offline Offline

Posts: 1696064024

View Profile Personal Message (Offline)

Ignore
1696064024
Reply with quote  #2

1696064024
Report to moderator
1696064024
Hero Member
*
Offline Offline

Posts: 1696064024

View Profile Personal Message (Offline)

Ignore
1696064024
Reply with quote  #2

1696064024
Report to moderator
The block chain is the main innovation of Bitcoin. It is the first distributed timestamping system.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1696064024
Hero Member
*
Offline Offline

Posts: 1696064024

View Profile Personal Message (Offline)

Ignore
1696064024
Reply with quote  #2

1696064024
Report to moderator
SgtSpike (OP)
Legendary
*
Offline Offline

Activity: 1386
Merit: 1005



View Profile
February 12, 2012, 09:37:24 PM
 #2

Bump?
theymos
Administrator
Legendary
*
Offline Offline

Activity: 4956
Merit: 11525


View Profile
February 13, 2012, 01:13:07 AM
 #3

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.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
SgtSpike (OP)
Legendary
*
Offline Offline

Activity: 1386
Merit: 1005



View Profile
February 13, 2012, 03:01:46 AM
 #4

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], 26);
 
// 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_database64_decode($secret), true)),
);
 
// our curl handle (initialize if required)
static $ch null;
if (is_null($ch)) {
$ch curl_init();
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($chCURLOPT_URL'https://mtgox.com/api/'.$path);
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
curl_setopt($chCURLOPT_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($restrue);
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.
theymos
Administrator
Legendary
*
Offline Offline

Activity: 4956
Merit: 11525


View Profile
February 13, 2012, 04:56:31 AM
 #5

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.)

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
SgtSpike (OP)
Legendary
*
Offline Offline

Activity: 1386
Merit: 1005



View Profile
February 13, 2012, 05:20:23 AM
Last edit: February 13, 2012, 05:33:51 AM by SgtSpike
 #6

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!