Marcus J.Kennedy
Marcus J.Kennedy

Reputation: 678

Decode php://input json

I have this text from file_get_contents('php://input'):

[{"email":"[email protected]","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<[email protected]>","timestamp":16813363}]

I need to get single element like email or event, but haven't been able to get json_decode and others to work.

$obj = json_decode(file_get_contents('php://input'));

How can I reference a single element in the json data?

Upvotes: 0

Views: 1745

Answers (2)

Chris
Chris

Reputation: 4728

Here you go, here's a fully working answer:

<?php

$str = '[{"email":"[email protected]","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<[email protected]>","timestamp":16813363}]';

$obj = json_decode($str);

echo $obj[0]->email;

Upvotes: -2

Felippe Duarte
Felippe Duarte

Reputation: 15131

You have an array, so, you need to get the first element:

$json = '[{"email":"[email protected]","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<[email protected]>","timestamp":16813363}]';

$obj = json_decode($json);
echo $obj[0]->email; //output [email protected]

Upvotes: 5

Related Questions