Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

第 18 章 React - Event-driven, non-blocking I/O with PHP. (Node.PHP)



This simple web server written in React responds with "Hello World" for every request.

require 'vendor/autoload.php';

$app = function ($request, $response) {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end("Hello World\n");

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at\n";

To install, just run this command:

% composer init --require=react/http:0.3.* -n
% composer install
To run the server, put the code into a file example.php and execute it with the php program:

% php example.php
Server running at