regex - PHP parse PUT request -
i've spent hours trying php parse put request key/value pairs.
the request coming out of ember data , of type form-data
, can't change (ember data doesn't allow far know). don't want install php extension (limits hosting options) or use php framework.
so, using postman, request looks 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 other browsers. i'm assuming different.
currently best this:
$input = file_get_contents('php://input'); $patten = ''; preg_match_all($patten, $input, $matches);
i wasn't able come patten close. think ideally search this:
name="
in between ---
if guys think form-data
structured differently on other browsers, maybe there better patten use.
after got $matches
back, planed on looping through them , merging $_request superglobal having correct key/value pairs, 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 add high level function in code, parse_put_vars_into_request
. ideally work delete , other http methods too.
i understand regex isn't ideal way solve problem, given needs work form-data
, it's best think of.
thanks!
as @zedfoxus pointed out, answer: http://www.chlab.ch/blog/archives/php/manually-parse-raw-http-data-php
but @steveax suggested making custom ember data adapter avoid using put's altogether: http://emberjs.com/api/data/classes/ds.adapter.html#method_updaterecord
both great answers. think i'll try out server side solution, because restful nature of get/post/put/delete.
Comments
Post a Comment