How do I print a specific JSON item in PHP?

Asked

Viewed 48 times

-1

<?php
        require __DIR__ . '/vendor/autoload.php';

        $accessToken = "----------------------------";


        use TotalVoice\Client as TotalVoiceClient;

        $client = new TotalVoiceClient($accessToken);

        $response = $client->perfil->consultaSaldo();

        echo $response->getContent(); // {}

?>


the return

{"status":200,"success":true,"reason":0,"message":"current balance","data":{"balance":2.47}}

but I want to take only the balance:2.47

2 answers

0

Quick fix:

<?php 

    $retorno='{"status":200,"sucesso":true,"motivo":0,"mensagem":"saldo atual","dados":{"saldo":2.47}}';

    $json=json_decode($retorno);
    var_dump($json);

    echo $json->dados->saldo;

    ?>

I got the json result, called the function json_decode to turn the return of json into a array, used a var_dump just to view the sequence of the array and then did the access sequence. Looking through var_dump you will understand when the logic used in the line $json->dados->saldo;

-1

        require __DIR__ . '/vendor/autoload.php';

        $accessToken = "----------------------------";


        use TotalVoice\Client as TotalVoiceClient;

        $client = new TotalVoiceClient($accessToken);

        $response = $client->perfil->consultaSaldo();

        $conteudo = $response->getContent();
        $json = json_decode($conteudo);
        echo "SALDO: ". $json->dados->saldo;  

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.