Bitcoin Forum
May 27, 2024, 10:48:58 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: API help needed - Easy answer for some of you I guess, But not for me :(  (Read 231 times)
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
April 17, 2022, 08:15:30 PM
 #1

I know this dosn't really belong here as it isn't specifically BTC but I shall be using the same for my BTC api calls, and you have been kind and helped me before.

This is an official IHost example and the address given are fictitious

Real quick one so I am trying to get a specific bit of information from an API.

It's an IHost api I am calling and i get a response similar to this:

Code:
{
    "result": {
        "deposits": [
            {
                "deposit_id": 36,
                "share_id": 2,
                "current_amount": 53.077553,
                "total_deposited": 24.91567,
                "total_earned": 28.146885,
                "withdraw_address": "GcUgnY5fFwTv9ef8rsggdxbdQzLEUxpp4c",
                "deposit_address": "GfUgnY5sFwTf9ez8rovtdxdEQzLcbxpb4c"
            },
            {
                "deposit_id": 37,
                "share_id": 5,
                "current_amount": 885.9591,
                "total_deposited": 521.92,
                "total_earned": 472.30566,
                "withdraw_address": "gHWHPs21H8UsSNcbfWxvn5XssAxFkcuZYe",
                "deposit_address": "g4sbWWtD3tf16Dsd8wiaJkar3zhJ82qNKP"
            },
            {
                "deposit_id": 38,
                "share_id": 6,
                "current_amount": 754.5115,
                "total_deposited": 548.52997,
                "total_earned": 416.25214,
                "withdraw_address": "LLqWFFJkNSog6VwsbPWEWE4KcXJrzB6t1K",
                "deposit_address": "LW5Vbt1gEkvVQzfVcpLmidLPpcED1Yp3yu"
            }
        ]
    },
    "error": ""
}

by using

Code:
print_r($obj->result->deposits);

BUT how do I just get a specific bit of info for example if I want to display total_earned on deposit ID38 what would I have to alter

Code:
print_r($obj->result->deposits);

to to make it work ?

Many Thanks for any help offered Smiley

TT
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
April 18, 2022, 04:24:04 AM
Merited by ABCbits (1)
 #2

This is a standard JSON response.

What you are looking to do is parse JSON in what appears to be PHP.

If you haven't already you can use json_decode() to convert it to an array and tell it to return the numeric key #2.

Deposit #38 is the third element in the object, but in programming zero is the first position -- therefore Deposit #38 is actually labeled #2 in the object.


Code:
$json = json_decode($obj, true);
echo $json["result"]["deposits"][2];

FuloosIO
Copper Member
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile WWW
April 19, 2022, 06:36:37 PM
 #3

The above response, is correct, however, to access the variables within each range, you would need to use

This would be accessing the first object in the array.
Code:
print_r($json['result']['deposits'][0]['share_id']);

This would be accessing the second object in the array.
Code:
print_r($json['result']['deposits'][1]['share_id']);

You could loop this code, quite easily for table format using a for each loop.

Code:
foreach($json['result']['deposits'] as $id => $info)
{
 #var_dump($info);
 echo $info['withdraw_address']; / /Example for accessing the data.
}
andresteves
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
August 08, 2022, 02:51:34 PM
 #4

Not sure if you already had an answer to the question but I guess what you wanted is(based on FuloosIo response):

Code:
foreach($json['result']['deposits'] as $id => $info) {

   if($info['deposit_id'] === 38) {
     echo $info['total_earned'];
   }
 
}
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!