0

I'm trying to write a REST API in PHP from the ground up for my website, partly as a learning exercise, partly to develop a codebase that I can reference later in case I forget how something works.

To my dismay, I've discovered that PHP has no $_PUT superglobal.

Remembering that Laravel makes GET/PUT/POST/DELETE distinctions, I figured Laravel must have code to handle HTTP PUT requests correcly, but no, in fact, it depends on a hidden form field with the value "_PUT" to specify the action to take.

Without the need to process files, is there any way to take multipart/form-data and parse it into an associative array in a similar fashion to $_POST, such that it is foreach iterable?

Here is what I tried and it simply doesn't work. I'm not understanding what the extra data is that is sent, must be related to the PHP session?

parse_str(file_get_contents("php://input"), $_PUT);
foreach ($_PUT as $key => $value)
{
    unset($_PUT[$key]);
    $_PUT[str_replace('amp;', '', $key)] = $value;
}
$_REQUEST = array_merge($_REQUEST, $_PUT);
foreach($_PUT as $key=>$value){
    $ani->state[$key]['value'] = $value;
}

What I end up getting out of this looks like this: (I am pretty sure I can beat this into what I want, but I don't think what I come up with is going to be robust enough to trust not to break all the time.)

What the server sees in the array after parsing php's stdin

So as not to anger anyone, the code I'm using came directly from here: https://joshtronic.com/2014/06/01/how-to-process-put-requests-with-php/

I tried to spin my own, which ended up looking very similar to his, minus merging the body back into the request, which I still don't completely understand the purpose of.

Robert Talada
  • 317
  • 1
  • 10
  • Maybe here is a similar question and answer: https://stackoverflow.com/a/5488449/1461181 – odan Aug 11 '19 at 20:11
  • @odan Thank you, but this ended up not being what I needed. I found a solution and will post an answer to my own question. – Robert Talada Aug 11 '19 at 20:28

1 Answers1

0

My solution is now simply to not use multipart/form-data and instead use application/x-www-form-urlencoded. I don't know if this decision will haunt me later on down the road but it solves my problem for now. I can still use this for my API as in JavaScript I can specify the content type as such:

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

I'd be very interested in knowing why PHP doesn't add a $_PUT superglobal?

Robert Talada
  • 317
  • 1
  • 10
  • PUT is meant as a method for "uploading" stuff to a particular URI, or overwriting what is already in that URI. POST, on the other hand, is a way of submitting data RELATED to a given URI. Read more: [PUT method support](https://www.php.net/manual/en/features.file-upload.put-method.php) – odan Aug 11 '19 at 21:03
  • @odan Right, 'overwriting' is essentially what I am doing. I see POST as creating the initial record or adding new data to it. Never overwriting existing data. Am I missing something? If my logic doesn't make sense here, I'm definitely open to revising it. I'm completely new to this. – Robert Talada Aug 12 '19 at 04:23