qq_41755278 2020-08-10 16:18 采纳率: 0%
浏览 214

Tp5.1整合Swoole无法跳转到方法!!!

图片说明

<?php

namespace app\server;

class http_server
{
    private $server = null;

    public function __construct()
    {
        $this->server = new \Swoole\Http\Server("0.0.0.0", 9501);
        $this->server->set([
            'worker_num'            => 5,
            'enable_static_handler' => true,
            'document_root'         => '/wwwroot/tp5.1/public/static',
        ]);
        $this->server->on('WorkerStart', [$this, 'onWorkerStart']);
        $this->server->on('request', [$this, 'onRequest']);
        $this->server->start();
    }

    public function onWorkerStart($server, $worker_id)
    {
        // 定义应用目录
        define('APP_PATH', __DIR__ . '/../application/');
        //加载框架引导文件
        require __DIR__ . '/../thinkphp/base.php';
    }

    public function onRequest($request, $response)
    {
        print_r($request->server);
        $_SERVER = [];
        if (isset($request->server)) {
            foreach ($request->server as $k => $v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        if (isset($request->header)) {
            foreach ($request->header as $k => $v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }

        $_GET = [];
        if (isset($request->get)) {
            foreach ($request->get as $k => $v) {
                $_GET[$k] = $v;
            }
        }

        $_POST = [];
        if (isset($request->post)) {
            foreach ($request->post as $k => $v) {
                $_POST[$k] = $v;
            }
        }

        ob_start();
        try {
            // 执行应用并响应
            \think\Container::get('app', [APP_PATH])->run()->send();
            //\think\Container::get('app')->run()->send();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
        echo "--action--".request()->action().PHP_EOL;#request()->action()完全是thinkphp5.1内置方法
        $response->header('content-type','text/html; charset=UTF-8');
        $res = ob_get_contents();#获取缓存数据
        ob_end_clean();
        $response->end($res);
    }
}

new  http_server();
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-08-11 10:36
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 支付行业技术人员来一个。常年包养
  • ¥15 kotlin multiplaform Compose项目 需要实现一个文件选择器获取手机上的文件
  • ¥15 Python没爬取到数据,求看这个是什么原因
  • ¥15 sqlite 读取数据如何避免float类型转化为指数形式
  • ¥15 IVX做拼图游戏,设置事件为“开始重叠”不弹target
  • ¥15 PHPCMS在表单提交之后对某一个字段进行二次联动并显示在前台提示语上
  • ¥50 Python调用Canalyzer发报文
  • ¥15 关于使用Azure Auto ML时上传的数据无法加载MLTable问题
  • ¥15 flume采集日志信息到hdfs
  • ¥15 电脑重启发现主机转屏幕不亮