Reputation: 7395
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
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