Alexander Trauzzi
Alexander Trauzzi

Reputation: 7395

PHP not parsing x-www-form-urlencoded data during non-POST requests

Is there a way to get PHP to automatically populate the $_POST superglobal array when request bodies are provided as x-www-form-urlencoded or multipart/form-data during a non-post request?

Currently, if I issue a PATCH request with a request body made up of either of the content types above, the data is never entered into a superglobal.

Upvotes: 1

Views: 3741

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 73031

I ran into a similar problem when building a RESTful API. The following is code which builds $requestData. To Orestes' point, I don't modify superglobals. Should get you started:

switch ($request_method) {
    case 'get':
        $requestData = $_GET;
        break;
    case 'post':
        $requestData = $_POST;
        break;
    case 'put':
    case 'delete':
        // read string from PHP's special input location and parse into an array
        parse_str(file_get_contents('php://input'), $requestData);      
        break;
}

Upvotes: 3

Related Questions