0

I want to connect my RPC wallet server with my WEB server using RPC AND PHP libary for my litecolin daemon server.

Here is my libary which i use to connect with my server.

[LITECOIN PHP LIBARY][1] [1]:

Here is my index.php which will generate the new address to deposit the litecoin coins :)

$litecoind = new LitecoinClient([
    'scheme' => 'http',                 // optional, default http
    'host'   => '1HIDDEN.HIDDEN.HIDDEN.0',            // optional, default localhost
    'port'   => PRIVATE,                   // optional, default 9332
    'user'   => 'HIDDEN',              // required
    'pass'   => 'HIDDEN',          // required

]);

$alo=$litecoind->getnewaddress();
?>

<pre>
<?php 
print_r($alo);
?>
</pre>

So when i get response i get some very hard array to read for me and from him i only need the variable which is in this block as [response]

     [container:protected] => Array
            (
                [result] => MUZiKwDneYD7a6G8Sx3TQjVZqfC3JkDobu
                [error] => 
                [id] => 0
            )

Here is the full response from the server printed as pretty print:

Majestic\Litecoin\LitecoindResponse Object
(
    [response:protected] => GuzzleHttp\Psr7\Response Object
        (
            [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
            [statusCode:GuzzleHttp\Psr7\Response:private] => 200
            [headers:GuzzleHttp\Psr7\Response:private] => Array
                (
                    [Content-Type] => Array
                        (
                            [0] => application/json
                        )

                    [Date] => Array
                        (
                            [0] => Mon, 08 Jun 2020 17:09:32 GMT
                        )

                    [Content-Length] => Array
                        (
                            [0] => 68
                        )

                )

            [headerNames:GuzzleHttp\Psr7\Response:private] => Array
                (
                    [content-type] => Content-Type
                    [date] => Date
                    [content-length] => Content-Length
                )

            [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
            [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #44
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                        (
                        )

                )

        )

    [container:protected] => Array
        (
            [result] => MNTRoGELAMYRLm395Yj2sWYTPrnGi6URwz
            [error] => 
            [id] => 0
        )

    [current:protected] => 
)

This is very hard for someone who don't understand json responses to understand how to take only [response] and save it to sql, of course i know how to save to mysql but how to take only that response variable is hard for me so I want to ask anyone who knows what to do here to help me or even point me to some easy solution.

Thank you so much for reading my code. Thank you for your future response and help!

your stack friend :)

EDIT: If this is not json response could anyone tell me what is this response and how to get result (wallet) variable which i need. When i want to echo my $alo variable i got the error that object can not be converted to string... what

ADyson
  • 57,178
  • 14
  • 51
  • 63
Ajmoopetpokusat
  • 147
  • 1
  • 9
  • 1
    "If this is not json response"...it's possible it might have been JSON when it came from the remote server but it looks like the library has already parsed the JSON and turned it into a regular PHP object. So you can use it like any other PHP object, and access its properties and methods. The print_r command you've used simply prints out the structure of a PHP object, so that you can see all the properties of it, and then you know what is available to access. If you know how to work with PHP objects then you should know, or at least have a basic idea, of how to get information from this one. – ADyson Jun 08 '20 at 17:19
  • 1
    It's a decoded response. If you add `$alo = json_encode($alo);` it will get encoded in JSON format. – c0de b0unty Jun 08 '20 at 17:20
  • @ADyson The thing is that i know how to print object but when i do simple thing here like this : $alo=$litecoind->getnewaddress(); and then i do the echo $alo; I got this error which is strange for me: Recoverable fatal error: Object of class Majestic\Litecoin\LitecoindResponse could not be converted to string in index.php on line 20 Why?! – Ajmoopetpokusat Jun 08 '20 at 17:24
  • 1
    that's because you can't echo an object - what do you think PHP should print, exactly, to display the **whole** object, which includes constructors, methods, properties, etc? That's why debugging tools such as print_r and var_dump exist, to display that kind of info about the code objects. If you want to print some specific items of data from the object, then you need to refer to those items directly. – ADyson Jun 08 '20 at 17:34
  • How i can know which is the right method to get only address or result from this var dump array?! – Ajmoopetpokusat Jun 08 '20 at 17:35
  • 1
    Learn properly how to manipulate PHP objects, would be the first step! It's going to be an important programming skill for you. But the biggest problem here is that `container` is a "protected" property. This means it isn't accessible outside the class (or classes which inherit from it) - you should also learn about [visibility](https://www.php.net/manual/en/language.oop5.visibility.php) as well! – ADyson Jun 08 '20 at 17:43
  • 1
    According to the [source code of the class](https://raw.githubusercontent.com/majestic84/php-litecoinrpc/master/src/LitecoindResponse.php) it should be possible to call the `result()` function to return the "result" portion of the container object. e.g. `echo $alo->result();` – ADyson Jun 08 '20 at 17:45
  • @ADyson , THANK YOU MAN! You solved my issue ;) thanks!!!! – Ajmoopetpokusat Jun 08 '20 at 17:57
  • 1
    No problem! A little bit of research always pays off. I've added this as a full answer below, so you may accept / upvote it. Thanks. – ADyson Jun 08 '20 at 18:04

2 Answers2

1

You can access it like this

$myval = $alo["container"];
foreach($myval as $myvals){
var_dump($myvals);
}

c0de b0unty
  • 108
  • 9
  • very nice but i still don't get response as result.. Warning: Invalid argument supplied for foreach() in index.php on line 22 – Ajmoopetpokusat Jun 08 '20 at 17:22
  • 1
    @Ajmoopetpokusat add `var_dump($myval);` before foreach and comment out foreach loop. – c0de b0unty Jun 08 '20 at 17:26
  • 1
    @Ajmoopetpokusat Can you add `$alo = json_encode($alo);` before foreach `echo` the response and add it in your answer? – c0de b0unty Jun 08 '20 at 17:28
  • 1
    @C0deB0untyHunter 1) `container:protected` isn't the name of the property. Please make sure you understand how to interpret the output from `print_r` commands. 2) the property is `protected` which means it isn't accessible from outside the class. So for those reasons this code cannot work. – ADyson Jun 08 '20 at 17:42
  • $alo=$litecoind->getnewaddress(); echo $alo; so i must put it in class to get echo of $alo? – Ajmoopetpokusat Jun 08 '20 at 17:43
  • I dont understand how to echo protected property? can you help me please . @ADyson i will mark as answer just to get the response... – Ajmoopetpokusat Jun 08 '20 at 17:46
  • 1
    @Ajmoopetpokusat I already replied in the other comments above, with something you may find useful. Take a look please. – ADyson Jun 08 '20 at 17:48
1

The biggest problem here is that container is a "protected" property. This means it isn't accessible outside the class (or classes which inherit from it) - this is described in the PHP documentation here: https://www.php.net/manual/en/language.oop5.visibility.php .

However, although the documentation of the library neglects to mention it, according to the source code of the class it should be possible to call the result() function to return the "result" portion of the container object.

e.g.

echo $alo->result();

For reference, the result() function looks like this in the source code of the LitecoindResponse class:

/**
 * Gets result array.
 *
 * @return array|null
 */
public function result()
{
    if ($this->hasResult()) {
        return $this->container['result'];
    }
}
ADyson
  • 57,178
  • 14
  • 51
  • 63