Skip to content
Advertisement

Cant access JSON data with PHP

im trying to access the ID and SENT from this JSON but is not working for me.

UPDATED

$json = '{
 "response": {
  "sent": true,
  "message": "Sent to 57304",
  "id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
 }
}';

json_decode($json);

$id=$json->id;
$sent=$json->sent;

echo "</br> id:".$id."</br>";
echo "</br> sent:".$sent."</br>";

Advertisement

Answer

Use the php json_decode() function. Like this:

$json = '{
   "response": {
   "sent": true,
   "message": "Sent to 57304",
   "id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
   }
}';

$json = json_decode($json, true);
$json = (object) $json['response'];
echo $json->sent;  // output: 1
echo $json->id;    // output: "gBEGVzBChXFYAgmcOrfFpGem8qw"

1 is the equivalent for a boolean value of true

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement