I don’t know what the effect of setting the header is at that stage, as it’s an outgoing response element.
The input is raw text, this works for me:
<?php
$data = file_get_contents("php://input");
// Save a raw copy of the message in a debug directory
// this will fill up quickly so don't leave it turned on
//$pathNFile = "debug/".date('YmdHis')."-".uniqid().".txt";
//file_put_contents($pathNFile, $data);
$json = json_decode($data, true);
...