为什么ob_get_contents()获取不到数据呢

来源:7-5 让Swoole完美支持TP5(下)

慕少5137873

2019-11-03

<?php
/**
 *User
 */
$http_server = new Swoole\Http\Server("0.0.0.0", 8811);

$http_server->set(
    [
        'enable_static_handler' => true,
        'document_root' => '/data/wwwroot/www/tp5/public/static',
        'worker_num' => 5,
    ]
);
$http_server->on('WorkerStart', function (swoole_server $server, $server_id) {
    require __DIR__ . '/../thinkphp/base.php';
});
$http_server->on('request', function ($request, $response) use ($http_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;
        }
    }
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }
    ob_start();
    try {
        think\Container::get('app')->run()->send();
    } catch (\Exception $e) {
        //
        print_r("<pre>");
        var_dump($e);
        print_r("<pre/>");
    }
    //echo request()->action().PHP_EOL;
    $res = ob_get_contents();
    var_dump($res);
    if (ob_get_length() > 0) {
        ob_end_clean();
    }
    $response->end($res);
    //$http_server->close();
});

$http_server->start();

图片描述
打印的是false,但是index方法里面的东西输出到xshell页面了

写回答

1回答

singwa

2019-11-04

您好。TP 和 PHP 分别是哪个版本?

0
3
weixin_慕莱坞4107431
回复
慕少5137873
您的问题如何解决的 我这边也遇到了相同的问题
2020-09-14
共3条回复

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程