Created
April 19, 2023 05:41
-
-
Save developersharif/2ef073d9a2f8f246e039a5c3f5a40d64 to your computer and use it in GitHub Desktop.
PHP-Swoole-http-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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