首页 > 技术文章 > swoole之建立 http server

cshaptx4869 2019-05-05 15:12 原文

一、代码部分

 

<?php
/**
 * 传统:nginx <-> php-fpm(fast-cgi process manager) <-> php
 * swoole:http-request -> swoole
 */
use Swoole\Http\Server;

// 0.0.0.0 表示监听 外网地址 内网地址 本机地址
$http = new Server("0.0.0.0", 8888);

// 若请求的是静态内容则直接返回
$http->set([
    'enable_static_handler' => true,
    'document_root' => '/usr/local/nginx/html'
]);

$http->on('request', function ($request, $response) {
    // 设置cookie
    $response->cookie('swoole', 'http_server', time()+1800);
    $msg = print_r($request->get??'no param', true);
    $response->end("<h1> - {$msg} - </h1>");
});
$http->start();

 

可通过 命令行

# curl http://127.0.0.1 8888

或者

浏览器 http://127.0.0.1:8888

进行测试

 

 

推荐阅读