0

This issue is not the same and questions/answers about $_POST do not apply to this question since no form is involved. (I am using file_get_contents)

I am using application/json to send POST variables and when i dump the headers the content is there (although it is a string and not a JSON or array for some reason):

var_dump(file_get_contents('php://input'));

results in:

string(77) "string(63) "{"job_uid": 12345, "dummy": 100}" "

the problem is this:

no matter what i do i cannot reference the data as a JSON or an ARRAY

i would like to use one of the following techniques to reference the vars:

$data->job_uid
// or
$data['job_uid']

i have tried json_encode and decode, explode, and every other idea

other than parsing the string manually every time i cannot seem to get to the vars and data easily and i cannot figure out why.

any ideas?

scott
  • 85
  • 7
  • All you need is `$data = json_decode(file_get_contents('php://input'));`. FYI, JSON _is_ a string format – Phil Sep 07 '22 at 01:39
  • _"This issue is not the same"_... I beg to differ. Please [edit] your question to explain very clearly how the [answer](https://stackoverflow.com/a/18867369/283366) in the duplicate doesn't solve your issue – Phil Sep 07 '22 at 14:08
  • when i use that command i am unable to see the data using typical JSON syntax. no variation of $data-> or $data->data-> works and i am unable to loop with foreach; when i add the TRUE param for assoc array i am unable to view the data using typical array syntax $data['data'][0].. etc... – scott Sep 07 '22 at 19:42
  • $data = file_get_contents('php://input'); $decoded_json = json_decode($data, false); echo $decoded_json->job_uid; (obviously this code should work but it does not work) – scott Sep 07 '22 at 20:12
  • Make sure you're only reading from `php://input` once. Please [edit] your question to show exactly the code you're using (don't leave anything out) and try some debugging like adding `var_dump($data, $decoded_json)`. Include the output from that debugging in your question – Phil Sep 07 '22 at 23:43

0 Answers0