4

I'm trying to run a simple Web app over a ReactPHP Web server, but I can't figure out where to get POST data coming from an HTML form. The server is defined as:

include 'vendor/autoload.php';

register_shutdown_function(function() {
    echo implode(PHP_EOL, error_get_last()), PHP_EOL;
});

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);
$http->on('request', function(React\Http\Request $request, React\Http\Response $response) {
    print_r($request);
    $response->writeHead(200, array('Content-type' => 'text/html'));
    $response->end('<form method="POST"><input type="text" name="text"><input type="submit" name="submit" value="Submit"></form>');
});
$socket->listen(9000);
$loop->run();

When I post some string using the HTML form, the $request object, when printed on the console, looks like:

React\Http\Request Object
(
    [readable:React\Http\Request:private] => 1
    [method:React\Http\Request:private] => POST
    [path:React\Http\Request:private] => /
    [query:React\Http\Request:private] => Array
        (
        )

    [httpVersion:React\Http\Request:private] => 1.1
    [headers:React\Http\Request:private] => Array
        (
            [User-Agent] => Opera/9.80 (X11; Linux i686) Presto/2.12.388 Version/12.16
            [Host] => localhost:9000
            [Accept] => text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
            [Accept-Language] => it,en;q=0.9
            [Accept-Encoding] => gzip, deflate
            [Referer] => http://localhost:9000/
            [Connection] => Keep-Alive
            [Content-Length] => 24
            [Content-Type] => application/x-www-form-urlencoded
        )

    [listeners:protected] => Array
        (
        )

)

Here I can't find my data anywhere. I thought it should be located in the query property, but it's empty.

When I make GET requests, instead, data passed in the querystring can be found inside the query property of the $request object.

So, where can I find data passed with POST requests?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
swahnee
  • 2,661
  • 2
  • 24
  • 34
  • I suspect this is for an older version of ReactPHP - `React\Socket\Server` takes the loop at the second ctor parameter, not the first one. I believe 0.7.x is current at the time of writing. – halfer Jul 29 '17 at 18:03
  • For the current version of ReactPHP, the code to get a raw POST body is [in the subproject README](https://github.com/reactphp/http). This can be parsed into separate fields if it is regular key-value data. – halfer Jul 29 '17 at 18:11

1 Answers1

2

I repeat the last edit to my question here, so this question can be marked as answered.

Nevermind, found an answer here. Basically, it seems that React PHP does not support an easy way to read POST data yet. What we can do, though, is to read data as soon as it comes, watching the event data of the $request object:

$request->on('data', function($data) {
  // Here $data contains our POST data.
  // The $request needs to be manually ended, though.
});
swahnee
  • 2,661
  • 2
  • 24
  • 34
  • 1
    [link](https://github.com/reactphp/http/pull/13) You can follow this pull request and learn why it is that way. – Arun Poudel Apr 16 '15 at 20:26