The more I work with PHP, the crazier it becomes.
Since the JSON would be transmitted via POST, I assumed the following line would work:
You can imagine my surprise when the resulting decoding returned
$_POST turned out to be empty.
Naturally after this I set out to solve this issue and receive my data. That is when I came across this Stack Overflow post which was similar to my issue. As I looked through it and other sites, I kept coming across the following line, which I decided to test:
To my surprise, this worked! However, it makes no sense to me. The documentation on
file_get_contents() is about reading a file on the server or getting the contents of another website- not accessing data POSTed using AJAX! Where does
php://input come from? There is no mention of it in the documentation. Why does POSTed via AJAX data get piped through here instead of
$_POST as one might logically expect?
I still do not have answers for these questions, and if I did I am sure they would make more sense than PHP’s behavior here, but I wrote this post to remind me how to accessed data sent using this method but also so you can know and learn how to do it too, that is, if you ever need to do it. 🙂