本文實例講述了PHP swoole中http_server的配置與使用方法。分享給大家供大家參考,具體如下:
swoole中為我們提供了一個swoole_http_server類,方便我們處理http請求。
但是它對http協(xié)議的支持并不完整,所以一般建議在前面加一層nginx進行代理,對于php文件的處理交由swoole處理。
一、創(chuàng)建一個簡單的http服務(wù)
?php
//創(chuàng)建一個http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
$server->set([
'package_max_length' => 1024 * 1024 * 10,
//設(shè)置文件上傳的臨時目錄
'upload_tmp_dir' => __DIR__ . '/uploads/',
]);
//設(shè)置request事件回調(diào)
//函數(shù)有兩個參數(shù):
//swoole_http_request對象,負責(zé)http請求相關(guān)信息
//swoole_http_response對象,負責(zé)向客戶端響應(yīng)相關(guān)信息
$server->on('request', function (swoole_http_request $request, swoole_http_response $response) {
//請求的頭部信息
var_dump($request->header);
//請求相關(guān)的服務(wù)器信息,相當(dāng)于PHP中的$_SERVER
var_dump($request->server);
//請求的GET參數(shù),相當(dāng)于PHP中的$_GET
var_dump($request->get);
//請求的POST參數(shù),相當(dāng)于PHP中的$_POST
var_dump($request->post);
//請求的COOKIE信息
var_dump($request->cookie);
//文件上傳信息,文件大小不超過package_max_length的值
var_dump($request->files);
//獲取原始POST請求數(shù)據(jù),相當(dāng)于fopen('php://input');
var_dump($request->rawContent());
//獲取完整http請求報文
var_dump($request->getData());
//向客戶端發(fā)送信息
$response->end('hello');
});
//啟動服務(wù)
$server->start();
二、處理靜態(tài)文件
swoole中已經(jīng)幫我們內(nèi)置了兩個配置參數(shù),只需要簡單配置一下就可以實現(xiàn)。
不過功能簡易,不建議外網(wǎng)使用,有需求的可以自已實現(xiàn)。
?php
$server = new swoole_http_server('0.0.0.0', 8888);
$server->set([
//配置靜態(tài)文件根目錄
'document_root' => __DIR__ . '/statics/',
//開啟靜態(tài)文件請求處理功能,這樣當(dāng)請求的是一個靜態(tài)文件時,swoole自動會在上面配置的目錄中查找并返回
'enable_static_handler' => true,
]);
$server->on('request', function ($request, $response) {
});
$server->start();
三、處理文件上傳
?php
//創(chuàng)建一個http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
$server->set([
//文件上傳大小不超過該值
'package_max_length' => 1024 * 1024 * 50,
//設(shè)置文件上傳的臨時目錄
'upload_tmp_dir' => __DIR__ . '/tmp/',
'upload_dir' => __DIR__ . '/uploads/',
'document_root' => __DIR__ . '/statics/',
'enable_static_handler' => true,
]);
$server->on('request', function ($request, $response) use ($server) {
if ($request->server['path_info'] == '/upload') {
$tmp = $request->files['upload']['tmp_name'];
$upload = $server->setting['upload_dir'] . $request->files['upload']['name'];
if (file_exists($tmp)
move_uploaded_file($tmp, $upload)) {
$response->header('Content-Type', 'text/html; charset=UTF-8');
$response->end('上傳成功');
} else {
$response->end('上傳失敗');
}
}
});
//啟動服務(wù)
$server->start();
我們在statics目錄下創(chuàng)建一個upload.html文件:
!doctype html>
html lang="zh-CN">
head>
meta charset="UTF-8">
title>文件上傳/title>
/head>
body>
form action="/upload" method="post" enctype="multipart/form-data">
input type="file" name="upload" value="">
input type="submit" value="提交">
/form>
/body>
/html>
四、處理路由文件自動加載
?php
//創(chuàng)建一個http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
$server->set([
//配置項目的目錄
'project_path' => __DIR__ . '/src/',
]);
$server->on('WorkerStart', function ($server, $worker_id) {
//注冊自動加載函數(shù)
spl_autoload_register(function ($class) use($server) {
$class = $server->setting['project_path'] . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
if (file_exists($class)) {
include_once $class;
}
});
});
$server->on('request', function ($request, $response) use ($server) {
$pathInfo = explode('/', ltrim($request->server['path_info'], '/'));
//模塊/控制器/方法
$module = $pathInfo[0] ?? 'Index';
$controller = $pathInfo[1] ?? 'Index';
$method = $pathInfo[2] ?? 'index';
try {
$class = "\\{$module}\\{$controller}";
$result = (new $class)->{$method}();
$response->end($result);
} catch (\Throwable $e) {
$response->end($e->getMessage());
}
});
//啟動服務(wù)
$server->start();
我們在目錄 src 下創(chuàng)建 test 目錄,并創(chuàng)建 test.php 文件
?php
namespace Test;
class Test
{
public function test()
{
return 'test';
}
}
然后訪問 127.0.0.1:8888/test/test/test 就可以看到返回結(jié)果了。
通過$request->server['path_info']
來找到模塊,控制器,方法,然后注冊我們自已的加載函數(shù),引入文件。實例化類對象,然后調(diào)用方法,返回結(jié)果。
注意,不要將 spl_autoload_register 放到 onStart 事件回調(diào)函數(shù)中。
onStart 回調(diào)中,僅允許echo、打印Log、修改進程名稱。不得執(zhí)行其他操作。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php socket用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計算法總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- 詳解PHP Swoole長連接常見問題
- 詳解PHP Swoole與TCP三次握手
- 詳解PHP7開啟OPcache和Swoole性能的提升對比
- php中Swoole的熱更新實現(xiàn)代碼實例
- windows系統(tǒng)php環(huán)境安裝swoole具體步驟
- php使用Swoole實現(xiàn)毫秒級定時任務(wù)的方法
- php使用goto實現(xiàn)自動重啟swoole、reactphp、workerman服務(wù)的代碼
- PHP swoole的process模塊創(chuàng)建和使用子進程操作示例
- PHP swoole中使用task進程異步的處理耗時任務(wù)應(yīng)用案例分析
- 淺談swoole的作用與原理