Skip to content

Instantly share code, notes, and snippets.

@developersharif
Created April 19, 2023 05:41
Show Gist options
  • Save developersharif/2ef073d9a2f8f246e039a5c3f5a40d64 to your computer and use it in GitHub Desktop.
Save developersharif/2ef073d9a2f8f246e039a5c3f5a40d64 to your computer and use it in GitHub Desktop.
PHP-Swoole-http-server
<?php
// Create a new HTTP server on port 8080
$server = new Swoole\Http\Server("0.0.0.0", 4000);
// Define a route handler for the root path
$server->on("request", function ($request, $response) {
// Get the request path
$path = $request->server['request_uri'];
// Handle the route based on the path
if ($path == '/') {
$response->header("Content-Type", "text/plain");
$response->end("Hello, World!");
} else if ($path == '/about') {
$response->header("Content-Type", "text/plain");
$response->end("This is the about page.");
} else {
$response->header("Content-Type", "text/plain");
$response->status(404);
$response->end("Page not found.");
}
});
echo "Server started at http://localhost:4000\n";
// Start the server
$server->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment