PHP swoole中http_server的配置與使用方法實(shí)例分析
本文實(shí)例講述了PHP swoole中http_server的配置與使用方法。分享給大家供大家參考,具體如下:
swoole中為我們提供了一個(gè)swoole_http_server類,方便我們處理http請(qǐng)求。
但是它對(duì)http協(xié)議的支持并不完整,所以一般建議在前面加一層nginx進(jìn)行代理,對(duì)于php文件的處理交由swoole處理。
一、創(chuàng)建一個(gè)簡(jiǎn)單的http服務(wù)
<?php //創(chuàng)建一個(gè)http server服務(wù) $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ 'package_max_length' => 1024 * 1024 * 10, //設(shè)置文件上傳的臨時(shí)目錄 'upload_tmp_dir' => __DIR__ . '/uploads/', ]); //設(shè)置request事件回調(diào) //函數(shù)有兩個(gè)參數(shù): //swoole_http_request對(duì)象,負(fù)責(zé)http請(qǐng)求相關(guān)信息 //swoole_http_response對(duì)象,負(fù)責(zé)向客戶端響應(yīng)相關(guān)信息 $server->on('request', function (swoole_http_request $request, swoole_http_response $response) { //請(qǐng)求的頭部信息 var_dump($request->header); //請(qǐng)求相關(guān)的服務(wù)器信息,相當(dāng)于PHP中的$_SERVER var_dump($request->server); //請(qǐng)求的GET參數(shù),相當(dāng)于PHP中的$_GET var_dump($request->get); //請(qǐng)求的POST參數(shù),相當(dāng)于PHP中的$_POST var_dump($request->post); //請(qǐng)求的COOKIE信息 var_dump($request->cookie); //文件上傳信息,文件大小不超過(guò)package_max_length的值 var_dump($request->files); //獲取原始POST請(qǐng)求數(shù)據(jù),相當(dāng)于fopen('php://input'); var_dump($request->rawContent()); //獲取完整http請(qǐng)求報(bào)文 var_dump($request->getData()); //向客戶端發(fā)送信息 $response->end('hello'); }); //啟動(dòng)服務(wù) $server->start();
二、處理靜態(tài)文件
swoole中已經(jīng)幫我們內(nèi)置了兩個(gè)配置參數(shù),只需要簡(jiǎn)單配置一下就可以實(shí)現(xiàn)。
不過(guò)功能簡(jiǎn)易,不建議外網(wǎng)使用,有需求的可以自已實(shí)現(xiàn)。
<?php $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ //配置靜態(tài)文件根目錄 'document_root' => __DIR__ . '/statics/', //開啟靜態(tài)文件請(qǐng)求處理功能,這樣當(dāng)請(qǐng)求的是一個(gè)靜態(tài)文件時(shí),swoole自動(dòng)會(huì)在上面配置的目錄中查找并返回 'enable_static_handler' => true, ]); $server->on('request', function ($request, $response) { }); $server->start();
三、處理文件上傳
<?php //創(chuàng)建一個(gè)http server服務(wù) $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ //文件上傳大小不超過(guò)該值 'package_max_length' => 1024 * 1024 * 50, //設(shè)置文件上傳的臨時(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('上傳失敗'); } } }); //啟動(dòng)服務(wù) $server->start();
我們?cè)趕tatics目錄下創(chuàng)建一個(gè)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>
四、處理路由文件自動(dòng)加載
<?php //創(chuàng)建一個(gè)http server服務(wù) $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ //配置項(xiàng)目的目錄 'project_path' => __DIR__ . '/src/', ]); $server->on('WorkerStart', function ($server, $worker_id) { //注冊(cè)自動(dòng)加載函數(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()); } }); //啟動(dòng)服務(wù) $server->start();
我們?cè)谀夸?src 下創(chuàng)建 test 目錄,并創(chuàng)建 test.php 文件
<?php namespace Test; class Test { public function test() { return 'test'; } }
然后訪問(wèn) 127.0.0.1:8888/test/test/test 就可以看到返回結(jié)果了。
通過(guò)$request->server['path_info']
來(lái)找到模塊,控制器,方法,然后注冊(cè)我們自已的加載函數(shù),引入文件。實(shí)例化類對(duì)象,然后調(diào)用方法,返回結(jié)果。
注意,不要將 spl_autoload_register 放到 onStart 事件回調(diào)函數(shù)中。
onStart 回調(diào)中,僅允許echo、打印Log、修改進(jìn)程名稱。不得執(zhí)行其他操作。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php socket用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 詳解PHP Swoole長(zhǎng)連接常見問(wèn)題
- 詳解PHP Swoole與TCP三次握手
- 詳解PHP7開啟OPcache和Swoole性能的提升對(duì)比
- php中Swoole的熱更新實(shí)現(xiàn)代碼實(shí)例
- windows系統(tǒng)php環(huán)境安裝swoole具體步驟
- php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法
- php使用goto實(shí)現(xiàn)自動(dòng)重啟swoole、reactphp、workerman服務(wù)的代碼
- PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例
- PHP swoole中使用task進(jìn)程異步的處理耗時(shí)任務(wù)應(yīng)用案例分析
- 淺談swoole的作用與原理
相關(guān)文章
PHP實(shí)現(xiàn)的文件瀏覽器功能簡(jiǎn)單示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的文件瀏覽器功能,結(jié)合完整實(shí)例形式分析了php針對(duì)目錄與文件的遍歷、判斷、屬性讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09PHP實(shí)現(xiàn)把數(shù)字ID轉(zhuǎn)字母ID
以下是對(duì)使用PHP把數(shù)字ID轉(zhuǎn)字母ID的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08利用PHPExcel實(shí)現(xiàn)Excel文件的寫入和讀取
本篇文章主要介紹了利用PHPExcel實(shí)現(xiàn)Excel文件的寫入和讀取的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04PHP實(shí)現(xiàn)圖片的等比縮放和Logo水印功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)圖片的等比縮放和Logo水印功能,結(jié)合實(shí)例形式分析了php圖片等比例縮放及l(fā)ogo水印添加操作技巧,需要的朋友可以參考下2017-05-05docker?中搭建php環(huán)境經(jīng)驗(yàn)分享
這篇文章主要介紹了docker?中搭建php環(huán)境經(jīng)驗(yàn)分享的相關(guān)資料,需要的朋友可以參考下2023-09-09