<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 創建Web服務器 ## 程序代碼 http\_server.php ~~~ $http = new swoole_http_server("0.0.0.0", 9501); $http->on('request', function ($request, $response) { var_dump($request->get, $request->post); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"); }); $http->start(); ~~~ Http服務器只需要關注請求響應即可,所以只需要監聽一個`onRequest`事件。當有新的Http請求進入就會觸發此事件。事件回調函數有2個參數,一個是$request對象,包含了請求的相關信息,如GET/POST請求的數據。 另外一個是response對象,對request的響應可以通過操作response對象來完成。$response->end()方法表示輸出一段HTML內容,并結束此請求。 * `0.0.0.0`表示監聽所有IP地址,一臺服務器可能同時有多個IP,如`127.0.0.1`本地回環IP、`192.168.1.100`局域網IP、`210.127.20.2`外網IP,這里也可以單獨指定監聽一個IP * `9501`監聽的端口,如果被占用程序會拋出致命錯誤,中斷執行。 ## 啟動服務 ~~~ php http_server.php ~~~ * 可以打開瀏覽器,訪問`http://127.0.0.1:9501`查看程序的結果。 * 也可以使用apache`ab`工具對服務器進行壓力測試 ## Chrome 請求兩次問題 使用`Chrome`瀏覽器訪問服務器,會產生額外的一次請求,`/favicon.ico`,可以在代碼中響應`404`錯誤。 ~~~ $http->on('request', function ($request, $response) { if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') { return $response->end(); } var_dump($request->get, $request->post); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"); }); ~~~ ## URL 路由 應用程序可以根據`$request->server['request_uri']`實現路由。如:`http://127.0.0.1:9501/test/index/?a=1`,代碼中可以這樣實現`URL`路由。 ~~~ $http->on('request', function ($request, $response) { list($controller, $action) = explode('/', trim($request->server['request_uri'], '/')); //根據 $controller, $action 映射到不同的控制器類和方法 (new $contoller)->$action($request, $response); }); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看