Swoole入門(mén) - Web 服務(wù)

HTTP服務(wù)應(yīng)用廣泛,是我們必須掌握的。

常規(guī)的HTTP服務(wù)器的處理流程:

用戶發(fā)起請(qǐng)求nginx,nginx通過(guò)fpm發(fā)到php,php做最終的邏輯代碼執(zhí)行,然后返回給前端用戶。
fpm是FastCGI的進(jìn)程管理器,fpm會(huì)通過(guò)用戶配置管理一批的FastCGI進(jìn)程。
使用swoole的httpserver,就不再需要使用fmp,http直接請(qǐng)求swoole的httpserver,直接執(zhí)行PHP代碼邏輯。
Http服務(wù)器只需要關(guān)注請(qǐng)求響應(yīng)即可,所以只需要監(jiān)聽(tīng)一個(gè)onRequest事件。當(dāng)有新的Http請(qǐng)求進(jìn)入就會(huì)觸發(fā)此事件。事件回調(diào)函數(shù)有2個(gè)參數(shù),一個(gè)是$request對(duì)象,包含了請(qǐng)求的相關(guān)信息,如GET/POST請(qǐng)求的數(shù)據(jù)。

創(chuàng)建HTTP服務(wù)

<?php
$http = new Swoole\Http\Server("0.0.0.0", 8811);
$http->on('request', function ($request, $response) {
     $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();

nginx+swoole配置

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass   http://127.0.0.1:8811;
    }
localhost.png

另外一個(gè)是response對(duì)象,對(duì)request的響應(yīng)可以通過(guò)操作response對(duì)象來(lái)完成。$response->end()方法表示輸出一段HTML內(nèi)容,并結(jié)束此請(qǐng)求。

0.0.0.0 表示監(jiān)聽(tīng)所有IP地址,一臺(tái)服務(wù)器可能同時(shí)有多個(gè)IP,如127.0.0.1本地回環(huán)IP、192.168.1.100局域網(wǎng)IP、210.127.20.2 外網(wǎng)IP,這里也可以單獨(dú)指定監(jiān)聽(tīng)一個(gè)IP。
8811 監(jiān)聽(tīng)的端口,如果被占用程序會(huì)拋出致命錯(cuò)誤,中斷執(zhí)行。

設(shè)置文件根目錄

document_root:需要返回真正的html內(nèi)容而不是swoole中response->end()中的內(nèi)容;但是必須和 enable_static_handler 同時(shí)使用。

<?php
$http = new Swoole\Http\Server("0.0.0.0", 8811);
$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => '/opt/work/htdocs/swoole_mooc/demo/data'
    ]
);

$http->on('request', function ($request, $response) {
     $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();
1.html.png

ss:1.png

接收參數(shù)

<?php
$http = new Swoole\Http\Server("0.0.0.0", 8811);
$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => '/opt/work/htdocs/swoole_mooc/demo/data'
    ]
);

$http->on('request', function ($request, $response) {
    $response->end('params:'.json_encode($request->get));
});
$http->start();
$request->get.png
#### 接收指定參數(shù)
<?php
$http = new Swoole\Http\Server("0.0.0.0", 8811);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => '/opt/work/htdocs/swoole_mooc/demo/data'
    ]
);

$http->on('request', function ($request, $response) {
     $response->end('params:'.json_encode($request->get['name']));
});
$http->start();
$request->get['name'].png
設(shè)置cookie
<?php
$http = new Swoole\Http\Server("0.0.0.0", 8811);
$http->on('request', function ($request, $response) {
    $cookieValue = $request->get;
    $response->cookie('user', json_encode($cookieValue), time() + 1800);
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();
cookie.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容