phphtmljsonresponselitecoin

How to save one variable from rpc server response?


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


Solution

  • 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'];
        }
    }