0

I have the following object which is being posted to a PHP server.

payload={
    "sms_messages": [
        {
            "created": "Tue, 08 Jan 2013 23:25:08 +0000"
            "id": "5e8dd0d21e4b615e588e88848279634a",
            "from": "xxxxxxxxxxx",
            "destination": "xxxxxxxxxx",
            "message": "Hi. This is an inbound message"
        }
    ]
}

I want to echo the 'from' key however, I am not sure how to do this in PHP.

My guess would be something like this:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 echo $_POST['payload']['sms_messages']['from'];
}

However, this returns an error:

Illegal string offset 'sms_messages' 
cleverpaul
  • 805
  • 4
  • 11
  • 24

1 Answers1

0
$payload = json_decode(file_get_contents('php://input'));
if ($payload && isset($payload->sms_messages) && is_array($payload->sms_messages)) {
  foreach($payload->sms_messages as $msg) {
    // handle each message here, e.g. by accessing $msg->from
    print_r($msg); // debug output
  }
}
Jay
  • 2,532
  • 10
  • 16