NotionCommotion Posted December 30, 2016 Share Posted December 30, 2016 (edited) GET data seems pretty straight forward, but not so with POST data. It seems to work, however, that does not necessarily mean it is right. Am I doing it right? Thanks <?php require 'vendor/autoload.php'; use React\EventLoop\Factory; use React\Http\Server as HttpServer; use React\Socket\Server as SocketServer; set_time_limit(0); $port='1337'; //$host='0.0.0.0'; //$host='127.0.0.1'; $host='192.168.1.200'; //$host='192.168.1.201'; $loop = Factory::create(); $http_socket = new SocketServer($loop); $http = new HttpServer($http_socket); $http->on('request', function ($request, $response) { switch($request->getMethod()) { case 'GET': $response->writeHead(200, array('Content-Type' => 'text/plain')); $query = $request->getQuery(); echo print_r($query,1) . PHP_EOL; $response->end(print_r($query,1)); break; case 'POST': $requestBody = ''; $headers = $request->getHeaders(); $contentLength = (int)$headers['Content-Length']; $receivedData = 0; $request->on('data',function($data) use ($request, $response, &$requestBody, &$receivedData, $contentLength) { $requestBody.=$data; $receivedData+=strlen($data); if ($receivedData>=$contentLength) { parse_str($requestBody, $query); $response->writeHead(200, array('Content-Type' => 'text/plain')); echo print_r($query,1) . PHP_EOL; $response->end(print_r($query,1)); } }); break; } }); $http_socket->listen($port, $host); $loop->run(); Edited December 30, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/302839-receiving-post-data-in-react-http-server/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.