Home >PHP Framework >Swoole >Introducing swoole http_server to adapt to thinkphp 5.1

Introducing swoole http_server to adapt to thinkphp 5.1

coldplay.xixi
coldplay.xixiforward
2021-03-23 10:58:452500browse

Introducing swoole http_server to adapt to thinkphp 5.1

1. Environment description

  • thinkphp 5.1
  • swoole 4.0.2
  • Alibaba Cloud CentOS 7.4

Recommended (free): swoole

2. Development

1. Create a new server directory in the tp root directory to store swool_http_server.

http_server.php code code

<?php
/**
 * Created by PhpStorm.
 * Date: 2018/7/22
 * Time: 15:12
 */
$http = new swoole_http_server(&#39;0.0.0.0&#39;, 8811);

//set函数用于设置swoole_server运行时的各项参数
$http->set([
    &#39;worker_num&#39;=>4 ,//worker process num
]);

//此事件在Worker进程/Task进程启动时发生
$http->on(&#39;WorkerStart&#39;,function (swoole_server $server, $worker_id){
    // 定义应用目录
    define(&#39;APP_PATH&#39;, __DIR__ . &#39;/../application/&#39;);
    // 加载基础文件 ThinkPHP 引导文件
    require __DIR__ . &#39;/../thinkphp/base.php&#39;;

});

$http->on(&#39;request&#39;, function ($request, $response){

    if($request->server){
        foreach ($request->server as $key => $val){
            $_SERVER[strtoupper($key)] = $val;
        }
    }
    if($request->header){
        foreach ($request->header as $key => $val){
            $_SERVER[strtoupper($key)] = $val;
        }
    }
    if($request->get){
        foreach ($request->get as $key => $val){
            $_GET[$key] = $val;
        }
    }
    if($request->post){
        foreach ($request->post as $key => $val){
            $_POST[$key] = $val;
        }
    }
    ob_start();
    try{
        // thinkphp 执行应用并响应
        think\Container::get(&#39;app&#39;)
            ->run()
            ->send();
    }catch (\Exception $exception){
        // todo
    }
    $res = ob_get_contents();
    ob_end_clean();
    $response->end($res);
});

$http->start();

2. Enter the service directory, execute php http_server.php and start swoole_http_server

No error was reported and the startup was successful.

3. Create a new test method in index and access the server port 8811

##3. Question

.swoole will not log out $_GET $_POST.... Super global variables

The above is the detailed content of Introducing swoole http_server to adapt to thinkphp 5.1. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete