Avatar ·

How to get the body of a JSON POST request in PHP?

Hello everyone! I'm a beginner in PHP and trying to write a simple REST API. The client sends me a POST request with JSON data, but I can't seem to receive it on the server.\n\nHere's what I'm trying:\n\n```php\n $data]);\n?>\n```\n\nBut when sending a request via Postman with Content-Type: application/json header and body {"name": "John", "age": 30}, I get an error "Undefined index: data" or an empty array.\n\nI know that $_POST works with form-data and application/x-www-form-urlencoded, but what about JSON? I tried using file_get_contents('php://input'), but I'm not sure if that's correct. Maybe I need to configure something in php.ini? Or are there other ways?\n\nPlease advise on the correct approach.