PHP code example of compwright / swoole-psr7

1. Go to this page and download the library: Download compwright/swoole-psr7 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/ */

    

compwright / swoole-psr7 example snippets

 php
 declare(strict_types=1);

use Slim\App;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Compwright\SwoolePsr7\SwooleServerRequestConverter;
use Compwright\SwoolePsr7\SwooleResponseConverter;
use Swoole\Http\Response;
use Swoole\Http\Request;

sponse->getBody()->write("Hello, " . $args['name']);
    return $response->withHeader('X-Powered-By','compwright');
});

$http = new swoole_http_server('0.0.0.0', 9501);
$http->on('start', function ($server) {
    echo "Swoole server started at http://127.0.0.1:9501\n";
});

$http->on('request', function (Request $request, Response $response) use ($serverRequestFactory, $app) {
    $psr7Request = $serverRequestFactory->createFromSwoole($request);
    $psr7Response = $app->handle($psr7Request);
    $converter = new SwooleResponseConverter($response);
    $converter->send($psr7Response);
});

$http->start();