1. Go to this page and download the library: Download utopia-php/http library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
utopia-php / http example snippets
useUtopia\DI\Container;
useUtopia\DI\Dependency;
useUtopia\Http\Http;
useUtopia\Http\Request;
useUtopia\Http\Response;
useUtopia\Http\Adapter\FPM\Server;
// Creating the dependency injection container
$container = new Container();
// Adding a user dependency to the container
$user = new Dependency();
$user
->setName('user')
->inject('request') // We can insert and use other injections as well
->setCallback(fn (Request $request) => $request->getHeader('x-user-id', 'John Doe'));
$container->set($user);
// Defining Route
Http::get('/hello-world')
->inject('request') // Auto-injected each request
->inject('response') // Auto-injected each request
->inject('user')
->action(
function(Request $request, Response $response, string $user){
$response
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache')
->json(['message' => 'Hello World', 'user' => $user]);
}
);
Http::setMode(Http::MODE_TYPE_PRODUCTION);
$http = new Http(new Server(), $container, 'America/New_York');
$http->start();
useUtopia\DI\Container;
useUtopia\Http\Http;
useUtopia\Http\Response;
useUtopia\Http\Adapter\FPM\Server;
Http::get('/')
->inject('response')
->action(
function(Response $response){
$response->send('Hello from PHP FPM');
}
);
$http = new Http(new Server(), new Container(), 'America/New_York');
$http->start();