0

I tried to make a server file for my ReactPHP app following this video but when I started up the server, it ran successfully, but when I made a simple http GET the response was "Error code 500: Internal server error", when in theory it should've returned a JSON {"message": "Hello"}.

Here is the code for the server.php file:

use React\Http\Server;
use React\Http\Response;
use Psr\Http\Message\ServerRequestInterface;
use \React\EventLoop\Factory;

require 'vendor/autoload.php';

$loop = Factory::create();
$server = new Server(function (ServerRequestInterface $request) {
  return new Response(
    200, ['Content-Type' => 'application/json'], json_encode(['message' => 'Hello'])
  );

});
$socket = new \React\Socket\Server('127.0.0.1:8000', $loop);
$server->listen($socket);

echo "Listening on ".str_replace('tcp', 'http', $socket->getAddress()). PHP_EOL;
$loop->run();

request.http file:

GET 127.0.0.1:8000

What the request has returned:

HTTP/1.1 500 Internal Server Error
Content-Type: text/plain
Server: ReactPHP/1
Date: Fri, 20 Aug 2021 09:03:19 GMT
Content-Length: 32
Connection: close

Error 500: Internal Server Error

Can someone say to me what the problem is? I think I miswrote something in the server.php file but I am not the one to tell

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Wulfo
  • 13
  • 3

2 Answers2

3

Code you use from a video is a bit outdated.

If you need a quickfix - just replace one line and it will work:

--- use React\Http\Response;
+++ use React\Http\Message\Response;

Working code for this example (as for react/http-1.5.0) would be

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;

require_once __DIR__ . '/vendor/autoload.php';

$loop   = Loop::get();
$server = new HttpServer(function (ServerRequestInterface $request) {
    return new Response(
        200, ['Content-Type' => 'application/json'], json_encode(['message' => 'Hello'])
    );
});
$socket = new SocketServer('127.0.0.1:8000');
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL;
$loop->run();

List of changes:

  • Response class location (actual fix)
  • loop factory changed, deprecation upFactory::create() -> Loop::get
  • http-server changed React\Http\Server -> React\Http\HttpServer
  • socket-server changed React\Socket\Server -> React\Socket\SocketServer
0

The answer provided by Ilya Urvachev is the right one, basically react evolved and the Response class is not located in the same location anymore.

Overall after instantiating your server I recommend you to use this line of code so you get error messages in your command line interface:

$server->on('error', function (Exception $exception) {
    echo $exception->getMessage() . PHP_EOL;
});

All in all, It's been very difficult for me to move from regular PHP to reactphp, which completely destroy the verbosity of error messages. Ie. You've got exception telling you that the format of the answer is not the expected one, while, what really fucks up your code is that have an error in the syntax of one of your MySQL queries, which sends back a message that is not respecting the expecting answer's format. If someone wants to provide additional way to improve the reactphp verbosity, feel free :)

Hugo Trial
  • 366
  • 5
  • 12