分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

Swoole ?实现路由功能的Http服务器

发布时间:2023-09-06 02:14责任编辑:傅花花关键词:暂无标签
 1 <?php 2 include_once ‘error.php‘;//文件代码在下方 3 $http=new \swoole\http\server(‘0.0.0.0‘,9502); 4 $http->set([ 5 ????‘package_max_length‘=>1024*1024*10, 6 ????‘upload_tmp_dir‘=>__DIR__.‘/upload‘ 7 ]); 8 //监听http协议 9 $http->on(‘request‘,function ($request,$response){10 ????$response->header(‘Content-Type‘,‘text/html‘);11 ????$response->header(‘Charset‘,‘utf-8‘);12 ????$server=$request->server;13 ????$path_info=$server[‘path_info‘];14 ????if($path_info==‘/‘){15 ????????$path_info=‘/‘;16 ????}else{17 ????????$path_info=explode(‘/‘,$path_info);18 ????}19 ????if(!is_array($path_info)) {20 ????????$response->status(404);21 ????????$response->end(‘<meta charset="UTF-8">请求路径无效‘);22 ????}23 ????//模块24 ????$model=(isset($path_info[1]) && !empty($path_info[1]))?$path_info[1]:‘Swooleman‘;25 ????//控制器26 ????$controller=(isset($path_info[2]) && !empty($path_info[2]))?$path_info[2]:‘error‘;27 ????//方法28 ????$method=(isset($path_info[3]) && !empty($path_info[3]))?$path_info[3]:‘index‘;29 ????//结合错误处理30 ????$class_name="\\{$model}\\$controller";31 ????if($class_name == ‘\favicon.ico\Index‘||$class_name == ‘\favicon.ico\error‘ ){32 ???????????return ;33 ????}34 ????//判断控制器类是否存在35 ???if(file_exists(__DIR__.‘/../‘.str_replace(‘\\‘, ‘/‘, $class_name).‘.php‘)){36 ???????require_once __DIR__.‘/../‘.str_replace(‘\\‘, ‘/‘, $class_name).‘.php‘;37 ???????$obj= new $class_name;38 ???????//判断控制器方法是否存在39 ???????if(!method_exists($obj,$method)){40 ???????????$response->status(404);41 ???????????$response->end("<meta charset=‘UTF-8‘>兄Dei,方法不存在,别瞎几把访问好吗");42 ???????}else{43 ???????????//如果存在此方法,返回结果,return 无效44 ???????????$response->end($obj->$method());45 ???????}46 ???}else{47 ???????$response->status(404);48 ???????$response->end("<meta charset=‘UTF-8‘>兄Dei,类不存在,别瞎几把访问好吗");49 ???}50 });51 //启动http服务器52 $http->start();
 1 <?php 2 ?3 register_shutdown_function(‘handleFatal‘); 4 //进程关闭时触发 5 ?6 //发送错误信息到某个设备\发送邮件 7 ?8 ?9 function handleFatal()10 {11 ????$error = error_get_last(); //获取最后的错误12 ????if (isset($error[‘type‘]))13 ????{14 ????????switch ($error[‘type‘])15 ????????{16 ????????????case E_ERROR :17 ????????????case E_PARSE :18 ????????????case E_CORE_ERROR :19 ????????????case E_COMPILE_ERROR :20 ????????????????$message = $error[‘message‘];21 ????????????????$file = $error[‘file‘];22 ????????????????$line = $error[‘line‘];23 ????????????????$log = "$message ($file:$line)\nStack trace:\n";24 ????????????????$trace = debug_backtrace();25 ????????????????foreach ($trace as $i => $t)26 ????????????????{27 ????????????????????if (!isset($t[‘file‘]))28 ????????????????????{29 ????????????????????????$t[‘file‘] = ‘unknown‘;30 ????????????????????}31 ????????????????????if (!isset($t[‘line‘]))32 ????????????????????{33 ????????????????????????$t[‘line‘] = 0;34 ????????????????????}35 ????????????????????if (!isset($t[‘function‘]))36 ????????????????????{37 ????????????????????????$t[‘function‘] = ‘unknown‘;38 ????????????????????}39 ????????????????????$log .= "#$i {$t[‘file‘]}({$t[‘line‘]}): ";40 ????????????????????if (isset($t[‘object‘]) and is_object($t[‘object‘]))41 ????????????????????{42 ????????????????????????$log .= get_class($t[‘object‘]) . ‘->‘;43 ????????????????????}44 ????????????????????$log .= "{$t[‘function‘]}()\n";45 ????????????????}46 ????????????????if (isset($_SERVER[‘REQUEST_URI‘]))47 ????????????????{48 ????????????????????$log .= ‘[QUERY] ‘ . $_SERVER[‘REQUEST_URI‘];49 ????????????????}50 ????????????????file_put_contents(__DIR__.‘/log.log‘,$log);//错误日志的输出51 ????????????????return $log;52 ????????????????//echo $log;53 ????????????default:54 ????????????????break;55 ????????}56 ????}57 }

Swoole ?实现路由功能的Http服务器

原文地址:https://www.cnblogs.com/zhengweizhao/p/9646229.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved