How to read a PHP Post Variable That’s an Object
Jul 25, 2021
It’s easy enough to read in the value of a PHP variable POST variable:
$meetingNumber = $_POST['meetingNumber'];
However I was passing (posting) in an object that had several parameters:
You can’t read in the inner-most value as-is. You also can’t just treat it as an array.
// does not work
$meetingNumber = $_POST['meetingData']['meetingNumber'];
The Solution
The values are being passed in as a JSON object. the trick is to decode the JSON, then extract the value from the array that’s returned:
$meetingData = json_decode(file_get_contents('php://input'), true);
$meetingNumber = $meetingData['meetingData']['meetingNumber'];