# 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>");
```
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0