1

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.

4

1 回答 1

2

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;
}
于 2013-07-03T15:53:56.293 回答