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.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
utopia-php / http example snippets
use Utopia\DI\Container;
use Utopia\Http\Http;
use Utopia\Http\Request;
use Utopia\Http\Response;
use Utopia\Http\Adapter\FPM\Server;
$resources = new Container();
Http::get('/hello-world') // Define Route
->inject('request')
->inject('response')
->action(
function(Request $request, Response $response) {
$response
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache')
->json(['Hello' => 'World']);
}
);
Http::setMode(Http::MODE_TYPE_PRODUCTION);
$http = new Http(new Server($resources), 'America/New_York');
$http->start();
use Utopia\DI\Container;
use Utopia\Http\Http;
use Utopia\Http\Response;
use Utopia\Http\Adapter\FPM\Server;
$resources = new Container();
Http::get('/')
->inject('response')
->action(
function(Response $response) {
$response->send('Hello from PHP FPM');
}
);
$http = new Http(new Server($resources), 'America/New_York');
$http->start();
use Utopia\DI\Container;
use Utopia\Http\Http;
use Utopia\Http\Request;
use Utopia\Http\Response;
use Utopia\Http\Adapter\Swoole\Server;
$resources = new Container();
Http::get('/')
->inject('request')
->inject('response')
->action(
function(Request $request, Response $response) {
$response->send('Hello from Swoole');
}
);
$http = new Http(new Server('0.0.0.0', '80', resources: $resources), 'America/New_York');
$http->start();
Http::get('/')
->param('name', 'World', new Text(256), 'Name to greet. Optional, max length 256.', true)
->inject('response')
->action(function(string $name, Response $response) {
$response->send('Hello ' . $name);
});