React PHP get POST data

*爱你&永不变心* 提交于 2019-12-22 08:36:37

问题


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?


回答1:


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.
});


来源:https://stackoverflow.com/questions/27758509/react-php-get-post-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!