0

I am looking into building my own custom small framework and trying to make it as flexible as possible, I have now got to the input process of the design and I am wondering how I could implement the "request" class and how to return POST params from a POST request, etc.

I noticed that a lot of frameworks have a request class which hook directly into the "php://input" stream which I have tried to do and would like to utilize, however the only problem is this returns a string instead of an object or array of value=params.

What may be the best way for me to implement a method which would give me access to the param values of a POST request? I could probably explode the "php://input" string however this would be messy, I also tried the parse_str() on the stream which worked however sadly this also has issues if one of those values is something along the lines of foo">bar< as it seems to break out of the object and leave me with a partial incomplete value of foo.

My ultimate solution would be something along the lines of:

$_POST contains foo=bar;arg=value;

class request {

function get($arg=null)(){

if(!arg){/* return all, both foo and arg accessible*/}

else

/*return single value of the param with the $arg text*/

}

$foo = request->get('foo'); // "bar"
$all = request->get() // foo=bar;arg=value;
print $all->arg; // "value"
wimpydoody
  • 29
  • 2
  • http://php.net/manual/en/function.parse-str.php But I would just use `$_GET` and `$_POST`. – AbraCadaver Oct 18 '16 at 18:27
  • What's wrong with `$_POST` ? `if (!$arg) return $_POST; else return $_POST[$arg];` – hlscalon Oct 18 '16 at 18:28
  • @AbraCadaver I have already tried this function however it doesn't seem too robust so I'm looking for alternatives, the problem with using the globals is that they aren't as flexible, $_POST in some cases returns NULL whereas the raw input stream does not. See: http://stackoverflow.com/questions/8893574/php-php-input-vs-post – wimpydoody Oct 18 '16 at 18:29
  • 1
    You need to give a real example of what doesn't work with `parse_str()`. – AbraCadaver Oct 18 '16 at 18:29
  • @AbraCadaver I gave an example, here is the result of another example along with the object. email input: `foo"> string(6) "foo"> string(0) "" }` – wimpydoody Oct 18 '16 at 18:32
  • And `$_POST` should always be populated (if your code is correct) if the content type is `application/x-www-form-urlencoded` or `multipart/form-data-encoded`. – AbraCadaver Oct 18 '16 at 18:33
  • No, the actual content from `php://input` – AbraCadaver Oct 18 '16 at 18:34
  • The content type is application/x-www-form-urlencoded. The value seems to make the result break out of the object because of it's symbols. – wimpydoody Oct 18 '16 at 18:35
  • The actual content from the php://input is string(39) "email=foo%22%3Eb%3Car&password=foo" – wimpydoody Oct 18 '16 at 18:37
  • So maybe it is broken when viewing in a browser because of `>` and `<` but array is fine https://3v4l.org/kTXAL – AbraCadaver Oct 18 '16 at 18:41
  • Look at http://stackoverflow.com/questions/5483851/manually-parse-raw-http-data-with-php if http://php.net/manual/en/function.parse-str.php is not working for your use case. Also, could be a multibyte issue http://php.net/manual/en/function.mb-parse-str.php – Kevin Oct 18 '16 at 18:44
  • It's definitely looking more like a browser support issue. – wimpydoody Oct 18 '16 at 18:57

0 Answers0