3

I'm receiving some JSON via a HTTP POST Callback to a PHP page and I'm having an issue parsing out the JSON. Here's an example of what the JSON data that is sent looks like:

[
  {
    "type"        : "message-received",
    "time"        : "2016-09-14T18:20:16Z",
    "description" : "Incoming message received",
    "to"          : "+12345678902",
    "message"     : {
      "id"            : "14762070468292kw2fuqty55yp2b2",
      "time"          : "2016-09-14T18:20:16Z",
      "to"            : ["+12345678902"],
      "from"          : "+12345678901",
      "text"          : "Hey, check this out!",
      "applicationId" : "93de2206-9669-4e07-948d-329f4b722ee2",
      "media"         : [
        "https://messaging.bandwidth.com/api/v2/users/{accountId}/media/14762070468292kw2fuqty55yp2b2/0/bw.png"
        ],
      "owner"         : "+12345678902",
      "direction"     : "in",
      "segmentCount"  : 1
    }
  }
]

I'm then processing this as follows:

$eventJSON = file_get_contents('php://input');
$event= json_decode( $eventJSON ); 
$eventType = $event->type;

but I'm not getting anything so far for my $eventType variable - I think the issue might be that the JSON is an array but I'm not sure how to handle this?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
user982124
  • 4,416
  • 16
  • 65
  • 140

1 Answers1

4

To parse json try

$eventType = $event[0]->type;

Refer :- How do I extract data from JSON with PHP? to know the difference between object properties and array elements

Ananth Sathvick
  • 144
  • 1
  • 11