国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP swoole中http_server的配置與使用方法實(shí)例分析

瀏覽:106日期:2022-09-11 09:47:32

本文實(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/’, //開(kāi)啟靜態(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 文件

<?phpnamespace 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ì)有所幫助。

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 杭锦旗| 绍兴市| 老河口市| 闸北区| 遂宁市| 剑河县| 丹巴县| 西林县| 临沂市| 镇江市| 高雄县| 任丘市| 太仆寺旗| 凉山| 浪卡子县| 永川市| 黄陵县| 苏州市| 拜泉县| 六枝特区| 普格县| 藁城市| 邳州市| 肥西县| 邳州市| 太和县| 米林县| 广东省| 海阳市| 连江县| 贵阳市| 沙河市| 榆树市| 彭水| 安多县| 永安市| 潮安县| 台南县| 南城县| 资中县| 堆龙德庆县|