swoole完美支持tp5

<?php

//http服务

use think\Container;

$http = new Swoole\Http\Server('0.0.0.0', 8811);

$http->set([
    'enable_static_handler' => true, //开启静态文件请求处理功能,需配合 document_root 使用 默认 false
    'document_root' => '/var/www/html/study/tp5/public/static',
    'worker_num' => 5
]);
$http->on('WorkerStart', function ($server, $workerId) {
    define('APP_PATH', __DIR__ . '/../application');

    // 加载基础文件
    require __DIR__ . '/../thinkphp/base.php';
});

$http->on('request', function ($request, $response) use ($http){

    $_SERVER = [];
    if(isset($request->server)) {
        foreach ($request->server as $key => $val) {
            $_SERVER[strtoupper($key)] = $val;
        }
    }

    if(isset($request->header)) {
        foreach ($request->header as $key => $val) {
            $_SERVER[strtoupper($key)] = $val;
        }
    }
    $_COOKIE = [];
    if(isset($request->cookie)) {
        foreach ($request->cookie as $key => $val) {
            $_COOKIE[$key] = $val;
        }
    }
    $_GET = [];
    if(isset($request->get)) {
        foreach ($request->get as $key => $val) {
            $_GET[$key] = $val;
        }
    }
    $_POST = [];
    if(isset($request->post)) {
        foreach ($request->post as $key => $val) {
            $_POST[$key] = $val;
        }
    }
    ob_start();
    try {
        // 执行应用并响应
        think\Container::get('app', [APP_PATH])->run()->send();
    } catch (\Exception $e) {
        //TODO
        $e->getMessage();
    }

    $result = ob_get_contents();
    if(ob_get_length() > 0) {
        ob_end_clean();
    }

//    $response->header('charset', 'utf-8');
    $response->end($result);
    $http->close();
});


//启动服务器
$http->start();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值