zoukankan      html  css  js  c++  java
  • Swoole 实现路由功能的Http服务器

     1 <?php
     2 include_once 'error.php';//文件代码在下方
     3 $http=new swoolehttpserver('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.icoIndex'||$class_name == 'favicon.icoerror' ){
    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)
    Stack trace:
    ";
    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']}()
    ";
    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 }
  • 相关阅读:
    cmder 基本配置和使用
    apache开启.htaccess及.htaccess的使用方法
    PHP 伪静态规则写法RewriteRule-htaccess详细语法使用
    Oracle创建分区表
    Oracle基础知识
    SQLPLUS
    linux上使用docker安装oracle
    使用IDEA创建可执行jar
    Hyper-V-问题整理
    spring的容器管理
  • 原文地址:https://www.cnblogs.com/zhengweizhao/p/9646229.html
Copyright © 2011-2022 走看看