Jepf Jepf - 1 year ago 137
JSON Question

Echo JSON response with PHP

I am trying to echo a portion of my JSON response from the Plaid API.

Here is the code:

$data = array(
$string = http_build_query($data);

//initialize session

//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute session
$exchangeToken = curl_exec($ch);
echo $exchangeToken;
$exchangeT = json_decode($exchangeToken);
echo $exchangeT['access_token'];
//close session

Here is the response:

{ "sandbox": true, "access_token": "test_fidelity" }

I also get a 500 Internal Server Error, which results from the echo $exchangeT line. I want to take the access_token portion of the JSON response, echo it to verify, and eventually save it to a database.

Answer Source

If you pass true as second parameter in your json_decode then you will get an array instead of object so change this

     $exchangeT = json_decode($exchangeToken, true);
// it wii output as $exchangeT = array('sandbox'=>true,'access_token'=>'test_fidelity');
        echo $exchangeT['access_token'];

for more info read

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download