Hello. First of all you will use json_decode, after that you have an object with the desired properties. Once you have them in hand, you can access via object or externalize the variables as requested. Follow example:
<?php
$json_string = '{
"access_token": "ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa",
"token_type": "Bearer",
"expires_in": 300,
"refresh_token": "ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73",
"scope": "user_account send_currency currency_exchange"
}';
$json_parse = json_decode($json_string);
echo "<pre>";
echo "access_token :" . $json_parse->access_token . PHP_EOL;
echo "token_type :" . $json_parse->token_type . PHP_EOL;
echo "expires_in :" . $json_parse->expires_in . PHP_EOL;
echo "refresh_token :" . $json_parse->refresh_token . PHP_EOL;
echo "scope :" . $json_parse->scope . PHP_EOL;
echo "</pre>";
// Transforma o escopo das variáveis acessíveis externamente
// O item deve ser um array, como o json_decode transforma em objeto
// precisamos transformar em array
extract((array) $json_parse);
echo "<pre>";
echo "access_token :" . $access_token . PHP_EOL;
echo "token_type :" . $token_type . PHP_EOL;
echo "expires_in :" . $expires_in . PHP_EOL;
echo "refresh_token :" . $refresh_token . PHP_EOL;
echo "scope :" . $scope . PHP_EOL;
echo "</pre>";
?>
Upshot:
access_token :ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa
token_type :Bearer
expires_in :300
refresh_token :ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73
scope :user_account send_currency currency_exchange
access_token :ab5f49438xfbc2df2a6a927a02b5c2e2442am982c71ee8re4aee1b2c64783ddc7cab4050ed05d6aa
token_type :Bearer
expires_in :300
refresh_token :ab4156db100f148b6cgd7e17097e1f1c25dcf32a53ec64w287c0bcc5b8f8aa2d0799a413567b8d73
scope :user_account send_currency currency_exchange
You can view the execution of this code at this link.
Ever tried to use
json_encode($texto);
?– novic
Yeah, so far it’s not working
– Wellington Alves