<?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();
swoole完美支持tp5
最新推荐文章于 2024-10-26 10:31:24 发布