I working on a php script that is useing cURL returning a JSON object.

It outputting like this

stdClass Object
(
[hash160] => e8bb296352b4926574363bba6603703589d12d25
[address] => 1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW
[n_tx] => 3
[n_unredeemed] => 3
[total_received] => 3000000
[total_sent] => 0
[final_balance] => 3000000
[txs] => Array
(
[0] => stdClass Object
(
[hash] => 3a6ee1f05b3eb402878f1ac28efb056caf8cea584181bd931024cf7ce1dfdb11
[ver] => 1
[vin_sz] => 1
[vout_sz] => 2
[size] => 258
[relayed_by] => 127.0.0.1
[tx_index] => 28108192
[result] => 0
[time] => 1349051111
[block_height] => 201313
[inputs] => Array
(
[0] => stdClass Object
(
[prev_out] => stdClass Object
(
[type] => 0
[addr] => 1GHYfp7X81X8pXBpmQZwFJcXCsoCAESNag
[value] => 39000000
[tx_index] => 28107939
[n] => 1
)

                            )

                    )

                [out] => Array
                    (
                        [0] => stdClass Object
                            (
                                [addr] => 1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW
                                [value] => 1000000
                                [type] => 0
                            )

                        [1] => stdClass Object
                            (
                                [addr] => 1GHYfp7X81X8pXBpmQZwFJcXCsoCAESNag
                                [value] => 37950000
                                [type] => 0
                            )

                    )

            )[/QUOTE]

The only important data in there is the [time] and [Value]

my script so far

<?php
$address = '1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW';
$address_info = get_address_info($address);
echo '<pre>';
print_r($address_info);
echo '</pre>';

	function get_address_info($address, $debug = false)
    {
            $url = 'http://blockchain.info/address/' . $address . '?format=json';

            if ($debug)
            {
                    echo 'Fetching URL: '.$url.'<br/>';
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $response = curl_exec($ch);
            curl_close($ch);

            if ($debug)
            {
                    echo '<pre>Curl Response: ';
                    print_r($response);
                    echo '</pre>';
            }

            return json_decode($response);
			echo '<pre>Curl Response: ';
                    print_r($address_info);
                    echo '</pre>';
    }
	?>[/QUOTE]

Im pretty new to php and having a hard time finding examples working with JSON objects.

Trying to first just display only the time and value in presentable formatting.

eventually Im looking to take these [time] and [vaule] and enter them into an sql database. Checking the timestamps and increasing the balance only on new vaules transactions.

    XMLnewbi;11014423 wrote:

    Im pretty new to php and having a hard time finding examples working with JSON objects.

    That's understandable, since as far as PHP considered there's no such thing as a "JSON object." An object is an object (is an object)... the syntax is the same no matter what "type" of object it is.

    As such, have you tried reading the PHP manual section dedicated to [man]OOP5[/man]?

      bradgrafelman;11014429 wrote:

      That's understandable, since as far as PHP considered there's no such thing as a "JSON object." An object is an object (is an object)... the syntax is the same no matter what "type" of object it is.

      As such, have you tried reading the PHP manual section dedicated to [man]OOP5[/man]?

      Im going though it now. i did some early examples on here.

      so if I just do a var_dump($address_info) how will it know what [time] is? do I need to define the structure of the object?

        XMLnewbi;11014433 wrote:

        so if I just do a var_dump($address_info) how will it know what [time] is? do I need to define the structure of the object?

        There's no structure of an object to be defined, no. [man]var_dump/man simply enumerates through all of the object's properties and outputs them in a pretty format.

        What you're wanting to do is directly access a specific property (the one named time). You use the '->' operator to access methods and properties of an object, e.g. $obj->propertyName or $obj->methodName().

          stdClass Object
          (
              [hash160] => e8bb296352b4926574363bba6603703589d12d25
              [address] => 1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW
              [n_tx] => 3
              [n_unredeemed] => 3
              [total_received] => 3000000
              [total_sent] => 0
              [final_balance] => 3000000
              [txs] => Array
              (
                  [0] => stdClass Object
                  (
                      [hash] => 3a6ee1f05b3eb402878f1ac28efb056caf8cea584181bd931024cf7ce1dfdb11
                      [ver] => 1
                      [vin_sz] => 1
                      [vout_sz] => 2
                      [size] => 258
                      [relayed_by] => 127.0.0.1
                      [tx_index] => 28108192
                      [result] => 0
                      [COLOR="#000080"][time] => 1349051111[/COLOR]
                      [block_height] => 201313
                      [inputs] => Array
                      (
                          [0] => stdClass Object
                          (
                              [prev_out] => stdClass Object
                              (
                                  [type] => 0
                                  [addr] => 1GHYfp7X81X8pXBpmQZwFJcXCsoCAESNag
                                  [COLOR="#000080"][value] => 39000000[/COLOR]
                                  [tx_index] => 28107939
                                  [n] => 1
                              )
                          )
                      )
                      [out] => Array
                      (
                          [0] => stdClass Object
                          (
                              [addr] => 1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW
                              [COLOR="#000080"][value] => 1000000[/COLOR]
                              [type] => 0
                          )
                          [1] => stdClass Object
                          (
                              [addr] => 1GHYfp7X81X8pXBpmQZwFJcXCsoCAESNag
                              [COLOR="#000080"][value] => 37950000[/COLOR]
                              [type] => 0
                          )
                      )
                  )
              )
          )

          so, are the highlighted properties the ones you want?

          In php, you access object properties with the [font=monospace]->[/font] operator. For example, to get the value of that "time" property, you'd use:

          <?php
          print $yourObject->txs->0->time;
          // outputs "1349051111"

          ---------EDIT---------
          whup... took too long with formatting... : )

            so, are the highlighted properties the ones you want?

            In php, you access object properties with the [font=monospace]->[/font] operator. For example, to get the value of that "time" property, you'd use:

            <?php
            print $yourObject->txs->0->time;
            // outputs "1349051111"

            ---------EDIT---------
            whup... took too long with formatting... : )[/QUOTE]

            yup thats what im looking for, pretty simple, I was letting the JSON trip me up.

            working with that;

            <?php
            $address = '1NDZyj5txmztkPVPCWFLQ185SprXxJfKjW';
            $address_info = get_address_info($address);
            echo '<pre>';
            print_r($address_info);
            echo '</pre>';

            	function get_address_info($address, $debug = false)
                {
                        $url = 'http://blockchain.info/address/' . $address . '?format=json';
            
                        if ($debug)
                        {
                                echo 'Fetching URL: '.$url.'<br/>';
                        }
            
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                        $response = curl_exec($ch);
                        curl_close($ch);
            
                        if ($debug)
                        {
                                echo '<pre>Curl Response: ';
                                print_r($response);
                                echo '</pre>';
                        }
            
                        return json_decode($response);
            			echo '<pre>Curl Response: ';
                                print_r($address_info);
                                echo '</pre>';
                }
            
            	?>

            <?php
            print $address_info->txs->0->time; // outputs "1349051111"
            ?>

            getting an syntax error.

            Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in /home/content/96/9855996/html/php_login_v2.3/myaccount.php on line 164

            perhaps "$address_info" is my problem?

              The "->0" was incorrect in traq's example above; $yourObject->txs is not an object, thus adding a '->' after it doesn't make sense. Instead, since it's an array (as noted by your print_r() output above), you'd use square brackets to access the desired index of that array (namely zero).

                there is is, thank you

                print_r($address_info->txs[0]->time);

                outputs 1349051111

                im getting there.

                Next step is getting it to display all the txs [all], going to fiddle with that.

                And can i use the same line, $address_info->txs[0]->time in an insert into SQL?

                  also,
                  print_r($address_info->txs[0]->value);

                  does not work, kinda expected that because of the structor of the JSON object.

                  going to be something along the lines?

                  print_r($address_info->txs[0][inputs][0][prev_out]->value);

                    getting the hang of [] and ->

                    print_r($address_info->txs[0]->inputs[0]->prev_out->value);

                    works

                      XMLnewbi;11014465 wrote:

                      Next step is getting it to display all the txs [all], going to fiddle with that.

                      Since the txs property contains an array of objects, you probably just need to use a [man]foreach/man loop to process each object.

                      XMLnewbi;11014465 wrote:

                      And can i use the same line, $address_info->txs[0]->time in an insert into SQL?

                      I'm not sure what you're asking... but a SQL query is nothing but a string in PHP, and yes, you can insert any string value into another string in PHP...

                      XMLnewbi;11014469 wrote:

                      also,
                      print_r($address_info->txs[0]->value);

                      does not work

                      Right... just like $address_info->txs[0]->BradGrafelmansFavoriteColor wouldn't work either, since neither value nor BradGrafelmansFavoriteColor are properties of the object contained at $address_info->txs[0] (assuming we're talking about the same object you showed us above in your print_r() output).

                      XMLnewbi;11014469 wrote:

                      going to be something along the lines?

                      print_r($address_info->txs[0][inputs][0][prev_out]->value);

                      No, because $address_info->txs[0] is an object, so appending square brackets after that doesn't make any sense. Plus, neither inputs nor prev_out are strings, so even if it was an array instead of an object, you would still be generating PHP error messages (since without quotes PHP would assume you're referring to constants named inputs and prev_out).

                        I'll just point out here that it's possible to tell json_decode() to return everything as arrays (so you typically end up with a mutli-D array) by setting its optional 2nd param to true, in case that would make life easier on you? (It's how I normally use it, anyway. 🙂 )

                          I'm not sure what you're asking... but a SQL query is nothing but a string in PHP, and yes, you can insert any string value into another string in PHP...

                          sorry that wasn't very clear; The next step of my project to use these returned values and add them up.
                          im trying to insert the object data into my sql database.

                          something like

                          INSERT INTO Deposits
                          VALUES ($address_info->txs[0]->inputs[0]->prev_out->value, value2, value3,...)

                            NogDog;11014487 wrote:

                            I'll just point out here that it's possible to tell json_decode() to return everything as arrays

                            D'oh! I forgot about that option (probably because I don't use JSON much at all).

                            @: You might want to check out that option; see the manual page for [man]json_decode/man for more info/examples.

                            XMLnewbi;11014491 wrote:

                            something like

                            INSERT INTO Deposits
                            VALUES ($address_info->txs[0]->inputs[0]->prev_out->value, value2, value3,...)

                            My point was that that if this:

                            $address_info->txs[0]->inputs[0]->prev_out->value

                            contains a string value, you can use it in the same way you could use a literal string:

                            "foobar"

                            The only potential difference might be in the syntax you use. For example, with such a long chain as in your example, I would either use string concatenation or [man]sprintf/man.

                            However, since we're talking about SQL queries, it would probably be even easier (since you are propertly sanitizing all your data as well, right?? 🙂) to just use a parameterized query since the code would be a lot cleaner, e.g.:

                            // skeleton example; lots of error checking/handling is missing
                            
                            // connect to db
                            $mysqli = new mysqli( "example.com", "user", "password", "database" );
                            
                            // prepare the query
                            $stmt = $mysqli->prepare( 'INSERT INTO Deposits (int_column, double_column, varchar_column) VALUES (?, ?, ?)' );
                            
                            // bind the three values
                            $stmt->bind_param( "i", 123456 );
                            $stmt->bind_param( "d", 123.456 );
                            $stmt->bind_param( "s", $obj->with[4]->layers->of->properties );
                            
                            // execute the query
                            $stmt->execute();

                              Ok im having trouble understanding the foreach () construct

                              <?php
                              $arr = array(1, 2, 3, 4);
                              foreach ($arr as &$value) {
                                  $value = $value * 2;
                              }
                              // $arr is now array(2, 4, 6, 8)
                              unset($value); // break the reference with the last element
                              ?> 
                              [code=php]
                              $arr as I understood could be an object or array. And the $Value is the string value in the array you want. ie $Vaule[2] would output 1,2,6,4
                              
                              In my object I have array (object1, object2,object3)
                              But the thing is, I don&#8217;t know how many object&#8217;s there are.
                              All I want to say is for each object print [value] and [time]
                              
                              
                              So this is where I&#8217;m at now.
                              
                              [code=php] 
                              foreach ($address_info as $inputs)
                              $inputs = $address_info->txs;
                              
                              { 
                              
                               print_r ($inputs->time);
                               print_r ($inputs->result);
                              
                               }
                              [code=php]
                                XMLnewbi wrote:

                                $arr as I understood could be an object or array. And the $Value is the string value in the array you want.

                                No, foreach($array as $Value) goes through each of the values in $array (hence the name "for each..."), putting that value into $Value each time.

                                foreach ($address_info as $inputs)
                                $txs = $inputs->txs;
                                
                                {
                                
                                print_r ($txs->time);
                                print_r ($txs->result);
                                
                                }
                                  Write a Reply...