【swoole http_server】


<?php class Http{ CONST PORT = 8090; CONST HOST = '0.0.0.0'; CONST ENABLE_STATIC_HANDLER = TRUE; CONST DOCUMENT_ROOT = '/www/wwwroot/swoole'; private $http = null; public function __construct() { $this->http = new swoole_http_server(self::HOST,self::PORT); $this->http->set( [ 'enable_static_handler'=>self::ENABLE_STATIC_HANDLER, 'document_root' =>self::DOCUMENT_ROOT, ] ); $this->http->on('request',[$this,'onRequest']); $this->http->start(); } public function onRequest($request,$response) { var_dump($request->get,$request->post); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello Swoole".rand(1000,9999)."</h1>"); } }; new Http();

启动服务

[root@iz2ze2rkosydwj6j4rmp7hz swoole]# php http_server.php 

在浏览器访问:http://39.105.181.143:8090/index.html

在浏览器访问:http://39.105.181.143:8090/

总结:

  可能存在的问题:

  服务器需要设置安全组,并且打开防火墙

  如果不能访问静态资源:

    可能原因是因为swoole版本太低(我第一个失败的版本是1.7.21,搞了很久都不行,最后安装了官方最新版本4.2.2一切ok

原文地址:https://www.cnblogs.com/fyandy/p/10055042.html