brucegregory Posted October 10, 2012 Share Posted October 10, 2012 So now I have a return from an API with more than one array and I need to decode it to PHP variables again. <?php function cryptoxchange_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( 'cryptokey: '.$key, 'cryptopayload: '.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://cryptoxchange.com/api/v0/account/balance/?stamp=$mt'.$path); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 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) echo 'MtGox'; echo '<br/>'; var_dump(cryptoxchange_query('')); //$values = (cryptoxchange_query('')); //echo "USDS: ".$values['Balance'].' BTCS: '.$values['BTC']; // trade example // var_dump(mtgox_query('0/buyBTC.php', array('amount' => 1, 'price' => 15)));\ ?> Output: array(5) { ["AccountNumber"]=> string(18) "xxxxxxxxxxx" ["Balance"]=> array(4) { ["USD"]=> string(11) "47.42805318" ["AUD"]=> string(10) "0.00000000" ["BTC"]=> string(10) "7.15424820" ["NMC"]=> string(10) "0.00000000" } ["ReturnCodes"]=> int(1) ["err"]=> string(0) "" ["stamp"]=> string(3) "$mt" } Sorry for all the questions, Than You! Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/ Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 Take a look at Jessica's answer in your other thread, try to understand what it does and how it works, then apply your newfound knowledge to this var_dump output. It's exactly the same problem, just with different data this time. Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384101 Share on other sites More sharing options...
brucegregory Posted October 10, 2012 Author Share Posted October 10, 2012 I tried and I get errors everytime. Can you give me another hint? Please Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384102 Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 What errors? We're not Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384104 Share on other sites More sharing options...
brucegregory Posted October 10, 2012 Author Share Posted October 10, 2012 What errors? We're not Notice: Array to string conversion in C:\xampp\htdocs\bitcoin\mtgox.php on line 100 Notice: Undefined index: BTC in C:\xampp\htdocs\bitcoin\mtgox.php on line 100 USDS: Array BTCS: Sorrry. Im not new to PHP, but I have no idea how to do this and Google isnt helping me much like it usually does. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384350 Share on other sites More sharing options...
brucegregory Posted October 10, 2012 Author Share Posted October 10, 2012 I can retrieve my account number correctly, but the other variables will not work with the same notation. Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384352 Share on other sites More sharing options...
Jessica Posted October 10, 2012 Share Posted October 10, 2012 As you can see from the var dump, the values are arrays. If you don't yet know how to use arrays, read the manual. Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384353 Share on other sites More sharing options...
ManiacDan Posted October 11, 2012 Share Posted October 11, 2012 Give him a fishing pole Jess, come on. Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384399 Share on other sites More sharing options...
Jessica Posted October 11, 2012 Share Posted October 11, 2012 This isn't that same guy is it? Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384488 Share on other sites More sharing options...
ManiacDan Posted October 11, 2012 Share Posted October 11, 2012 I don't know/care. I just wanted to make you grit your teeth for a second ;-) Quote Link to comment https://forums.phpfreaks.com/topic/269285-another-var_dump-api-question/#findComment-1384492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.