iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌟
Building a TLS-Enabled HTTP/1 Server with ReactPHP
Download packages using Composer
composer require react/http react/socket
Prepare the following code
server.php
<?php
require __DIR__ . '/vendor/autoload.php';
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return React\Http\Message\Response::plaintext(
"Hello World!\n"
);
});
$socket = new React\Socket\SocketServer(
'127.0.0.1:8080',
['tls' => [
'local_cert' => 'localhost.pem',
'local_pk' => 'localhost-key.pem'
]
]
);
$http->listen($socket);
echo "Server running at https://127.0.0.1:8080" . PHP_EOL;
Generate the certificate and private key with mkcert
mkcert localhost
Start the server and access it in your browser
php server.php
Discussion