PHP PHP getRequestJSON Function - Get Request Body as JSON Object

The getRequestJSON function in PHP allows you to retrieve the request body as a JSON object. It returns the JSON object or a default value on error.

function getRequestJSON($default = null)
{
    static $_contents = null;

    if ($_contents === null) {
        $_contents = file_get_contents('php://input');
    }

    return $_contents === false ? $default : json_decode($_contents);
}