<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國際加速解決方案。 廣告
                # swoole_http_server [TOC=2,3] --- ## **1.swoole_http_server** 從swoole-1.7.7-stable版本開始,swoole提供內置的http服務器swoole_http_server。swoole_http_server繼承自swoole_server,是一個完整的http服務器實現。<br> 示例: ```php $http = new swoole_http_server("127.0.0.1", 9501); $http->on('request', function ($request, $response) { $html = "<h1>Hello Swoole.</h1>"; $response->end($html); }); $http->start(); ``` ## **1.1.work_mode** swoole_http_server支持同步和異步兩種模式。<br> **同步模式:** 這種模式類似于nginx+php-fpm/apache,它需要設置大量worker進程來完成并發請求處理。Worker進程內可以使用同步阻塞IO,編程方式與普通PHP Web程序完全一致。<br> 與php-fpm/apache不同的是,客戶端連接并不會獨占進程,每一個Worker進程都會常駐內存,服務器依然可以應對大量并發連接。<br> **異步模式:** 在這種模式下,Worker進程內不允許使用任何阻塞式API,例如MySQL、redis、http_client、file_get_contents、sleep等。需要使用swoole提供的各種異步API來實現,如異步swoole_client,swoole_event_add,swoole_timer,swoole_get_mysqli_sock等API。 ## **1.2.callback** 注冊事件回調函數,與[swoole_server回調函數](https://github.com/LinkedDestiny/swoole-doc/blob/master/doc/02.swoole_server%E4%BA%8B%E4%BB%B6%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0.md)基本相同,不同之處是:swoole_http_server不接受onConnect/onReceive/onClose回調設置, 同時swoole_http_server額外接受2種新的事件類型onRequest/onMessage **onRequest** 描述:收到完整的http請求時的回調。<br> 函數原型:<br> ```php function onRequest(swoole_http_request $request, swoole_http_response $response); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | [swoole_http_request](#2swoole_http_request) $request | Http請求信息對象,包含了header/get/post/cookie等相關信息 | | [swoole_http_response](#3swoole_http_response) $response | Http響應對象,支持cookie/header/status等Http操作 | 說明:<br> 在收到請求時,通過`$request`對象獲取請求內容,然后通過`$response`發送響應。 **onMessage** 描述:收到一個WebSocket數據請求時的回調。<br> 函數原型:<br> ```php function onMessage(swoole_http_request $request, swoole_http_response $response); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | [swoole_http_request](#2swoole_http_request) $request | WebSocket數據對象,存放在[message]((#26message))屬性中 | | [swoole_http_response](#3swoole_http_response) $response | WebSocket響應對象,支持message方法發送數據 | 說明:<br> WebSocket協議中,`$request`,`$response`對象會常駐內存,[message](#34message)方法調用后并不會銷毀這2個對象<br> (PS: 1.7.7版本還不支持WebSocket,因此這個回調無法正常工作。) ## **2.swoole_http_request** swoole_http_request是Http請求信息對象,包含了header/get/post/cookie等相關信息,在WebSocket模式下包含了message數據。 ### **2.1.header** 描述:<br> Http請求的頭部信息。 內容:<br> | key | 描述 | | -------- | ----- | | | | 說明: 類型為數組,所有key均為小寫。<br> ### **2.2.server** Http請求相關的服務器信息,相當于PHP的`$_SERVER`數組。 說明: 包含了Http請求的方法,URL路徑,客戶端IP等信息。數組的key全部為小寫,并且與PHP的`$_SERVER`數組保持一致。<br> ### **2.3.get** 描述:<br> Http請求的GET參數。<b4> 說明:<br> 相當于PHP中的$_GET,格式為數組。<br> > 1.7.7版本暫時不支持PHP的GET參數合并,比如`hello[]=1&hello[]=2`這樣的參數<br> 為防止HASH攻擊,GET參數最大不允許超過128個 ### **2.4.post** 描述:<br> Http請求的POST參數。<b4> 說明:<br> 相當于PHP中的$_POST,格式為數組。<br> > POST與Header加起來的尺寸不得超過package_max_length的設置,否則會認為是惡意請求 POST參數的個數最大不超過128個 ### **2.5.cookie** 描述:<br> HTTP請求攜帶的COOKIE信息。<b4> 說明:<br> 相當于PHP中的$_COOKIE,格式為數組。<br> ### **2.6.message** 描述:<br> WebSocket發送來的數據。<b4> 說明:無<br> ## **3.swoole_http_response** ### **3.1.header** 描述:設置HTTP響應的Header信息。<br> 函數原型:<br> ```php public function swoole_http_response::header(string $key, string $value); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | string $key | Header的名稱 | | string $value | Header的值 | 說明:<br> header設置必須在[end](#35end)方法之前。<br> 示例:<br> ```php $response->header("content-type", "text/html"); ``` ### **3.2.cookie** 描述:設置HTTP響應的Cookie信息。<br> 函數原型:<br> ```php public function swoole_http_response::cookie(string $key, string $value = '', int $expire = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | string $key | cookie的名稱 | | string $value | cookie的值 | | int $expire | 有效期 | | string $path|路徑| 說明:<br> cookie設置必須在[end](#35end)方法之前。<br> 示例:<br> ```php $response->cookie("content-type", "text/html"); ``` ### **3.3.status** 描述:發送Http狀態碼。<br> 函數原型:<br> ```php public function swoole_http_response::status(int $http_status_code); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | int $http_status_code | 合法的HttpCode,如200,404等 | 說明:<br> cookie設置必須在[end](#35end)方法之前。<br> `$http_status_code`必須為合法的HttpCode,如200, 502, 301, 404等,否則會報錯<br> 示例:<br> ```php $response->status(200); ``` ### **3.4.message** 描述:通過WebSocket發送數據。<br> 函數原型:<br> ```php public function swoole_http_response::message(string $message, bool $binary = false); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | string $message | 待發送的數據 | | bool $binary | 是否為二進制數據,默認為false | 說明:<br> 該函數只能用在WebSocket握手成功的Http客戶端。<br> message方法不同于[end](#35end),它并不會銷毀request/response對象。<br> 示例:<br> ```php $response->message("Hello World"); ``` ### **3.5.end** 描述:發送Http響應體,并結束請求處理。<br> 函數原型:<br> ```php public function swoole_http_response::end(string $html); ``` 參數說明:<br> | 參數 | 描述 | | -------- | ----- | | string $html | 待發送的數據 | 說明:<br> end操作后將向客戶端瀏覽器發送HTML內容,并銷毀`$request`/`$response`對象<br> 如果開啟了KeepAlive,連接將會保持,服務器會等待下一次請求<br> 未開啟KeepAlive,服務器將會切斷連接<br> 示例:<br> ```php $response->end("<h1>Hello Swoole.</h1>"); ```
                  <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>

                              哎呀哎呀视频在线观看