1

I've spent hours trying to get PHP to parse a PUT request into key/value pairs.

The request is coming out of Ember Data and is of type form-data and I can't change that (Ember Data doesn't allow that as far as I know). I don't want to install a PHP extension (limits my hosting options) or use a PHP framework.

So, using Postman, the request looks like this:

------WebKitFormBoundarytb5fqcjpCsLTsDjp
Content-Disposition: form-data; name="phone_number"

1234567
------WebKitFormBoundarytb5fqcjpCsLTsDjp
Content-Disposition: form-data; name="legal_name"

Drew Baker
------WebKitFormBoundarytb5fqcjpCsLTsDjp
Content-Disposition: form-data; name="first_name"

Drew
------WebKitFormBoundarytb5fqcjpCsLTsDjp
Content-Disposition: form-data; name="last_name"

Baker
------WebKitFormBoundarytb5fqcjpCsLTsDjp--

I have no idea what it would look like from other browsers. I'm assuming it would look different.

Currently the best I could do was this:

$input = file_get_contents('php://input');
$patten = '';
preg_match_all($patten, $input, $matches);

I wasn't able to come up with a patten that was even close. But I think ideally it would search like this:

name=" then everything in between ---

If you guys think that form-data is structured differently on other browsers, maybe there is a better patten to use.

After I got $matches back, I planed on looping through them and merging everything into the $_REQUEST superglobal having all the correct key/value pairs, like so:

$put_vars = array(
   'phone_number'   => '1234567',
   'legal_name'     => 'Drew Baker',
   'first_name'     => 'Drew',
   'last_name'      => 'Baker'
);
$_REQUEST = array_merge($_REQUEST, $put_vars);

My plan is to add this as a high level function in my code, something like parse_put_vars_into_request. But ideally it would work for DELETE and other HTTP methods too.

I understand that regex isn't the ideal way to solve this problem, but given that it needs to work with form-data, it's the best I could think of.

Thanks!

Drew Baker
  • 14,154
  • 15
  • 58
  • 97
  • Have you tried to do `var_dump($_FILES);` to see if that global variable captured contents of form-data? Also [this blog](http://www.chlab.ch/blog/archives/php/manually-parse-raw-http-data-php) may help. – zedfoxus May 31 '15 at 05:09
  • ember-data does indeed allow you to customize the put. You'll want to create an adapter, extend the DS.Adapter class and create your own [updateRecord](http://emberjs.com/api/data/classes/DS.Adapter.html#method_updateRecord) method (and any others methods you need). Seems a lot saner than trying to parse the form data on the back end. – steveax Jun 01 '15 at 02:04
  • These are both great answers but as comments! – Drew Baker Jun 01 '15 at 17:12
  • Does PHP even parse the body of PUT requests? – Knight Jan 17 '19 at 18:21

1 Answers1

0

As @zedfoxus pointed out, this is the answer: http://www.chlab.ch/blog/archives/php/manually-parse-raw-http-data-php

But @steveax suggested just making a custom Ember Data adapter to avoid using PUT's altogether: http://emberjs.com/api/data/classes/DS.Adapter.html#method_updateRecord

Both are great answers. I think I'll try out the server side solution, because I like the RESTful nature of GET/POST/PUT/DELETE.

Drew Baker
  • 14,154
  • 15
  • 58
  • 97
  • If you roll your own adapter, you can keep it as a put, but have ember data format the request however you want it on the server (application/json instead of form data for instance). – steveax Jun 03 '15 at 14:05