0

I am working on a research project for my graduate program in finance regarding crypto currencies (bitcoin,etc) . I am trying to parse out individual objects from JSON responses produced by cryptsy (market for exchanging crypto currencies) api(https://www.cryptsy.com/pages/privateapi). I am using the API example code given by cryptsy which can be found in the URL provided above. I would like to grab my account balances for starters. your help here would be very much appreciated.

The responses I am getting look like this:

Array
(
    [success] => 1
    [return] => Array
        (
            [balances_available] => Array
                (
                    [42] => 0.00000000
                    [AGS] => 0.00000000
                    [ALN] => 0.00000000
                    [ALF] => 0.00000000
                    [AMC] => 0.00000000

My code I am trying to write not working so well:

$result = api_query("getinfo");
$json = file_get_contents($result);
$json2 = json_decode($json, true);
foreach($json2->attachments->balances_available AS $attach) {
    file_put_contents('test.txt', $attach, FILE_APPEND);

}

echo "<pre>".print_r($json2, true)."</pre>";

Error Message:

Warning: file_get_contents() expects parameter 1 to be a valid path, array given in /Users/Aditya/Desktop/PHP-1.php on line 45

Warning: Invalid argument supplied for foreach() in /Users/Aditya/Desktop/PHP-1.php on line 47

Any help would be very much appreciated, I have looked all over every forum and I am not a computer science guy. Once again thank you for your time and patience.

RustyShackleford
  • 3,462
  • 9
  • 40
  • 81
  • 1
    Please explain what "not working so well" means. Error message for accessing an array with property names? What gets stored in the file, what did you try to accomplish instead? – mario Aug 24 '14 at 12:34
  • Sorry so the error messages say (Parse error: parse error in /Users/Aditya/Desktop/PHP-1.php on line 72) but on line 72 I have commented out (//$result = api_query("calculatefees", array("ordertype" => 'Buy', 'quantity' => 1000, 'price' => '0.005'));) This came directly from the example provided in the example api. I dont know where the file gets stored or even if its being created, and I have not tried to accomplish anything instead..I am stuck..Please forgive me I am very new computer science. – RustyShackleford Aug 24 '14 at 14:39
  • So the code in your question is irrelevant then. It's impossible to tell why you got a syntax error there; the error message excerpt is shortened out. And your excerpt does not reveal much. See [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/q/18050071) then. You're on your own. – mario Aug 24 '14 at 14:43
  • You can always [edit your question](http://stackoverflow.com/posts/25471717/edit) to include relevant information. Don't paste code into comments. That's not readable. Else nobody will notice and have a second look at your questions – mario Aug 24 '14 at 21:04
  • Sorry about that Mario. I have corrected the code within the question now. Very new here sorry. Regardless my end goal is that I want to be able to make a call to say [42] and get the balance in my account. – RustyShackleford Aug 24 '14 at 21:10
  • Do I even need to store anything in a text file? – RustyShackleford Aug 24 '14 at 21:22

1 Answers1

1

The sample api_query method already returns an array:

$array = api_query("getinfo");

Therefore you need to apply neither file_get_contents nor json_decode on the $result variable.

It's a plain array at this point, so just [] accesses, no -> property traversal. I'm not sure it's wrapped in ["attachments"] anyway. See var_dump($array) for the actual structure.

You can just loop over it:

foreach ($array["attachments"]["balances_available"] as $key => $value) {
    print " $key == $value \n<br>";
}

If you want to store it into an file (you may wish to elaborate on the format), then append it likewise:

foreach (/* as seen above*/) {
    file_put_contents(
        'test.txt',
        " $key has $value balance \n",
        FILE_APPEND
    );
}
mario
  • 144,265
  • 20
  • 237
  • 291