File HttpServer.php
<?php /** * Created by PhpStorm. * User: mac * Date: 2019/9/13 * Time: 21:00 */ class HttpServer { public $http_serv; const PORT = 9501; const IP = "0.0.0.0"; public $static_handel_switch = true; public $document_root = '/www/swoole'; public function __construct() { $this->http_serv = new Swoole\Http\Server(self::IP,self::PORT); $this->http_serv->on("request",[$this,"onRequest"]); if($this->static_handel_switch == true){ $this->enableStaticHandel($this->document_root); / ** } } * 接受到客户端请求 * @param $request 请求 * @param $response 响应 */ public function onRequest($request,$response) { //var_dump($request->server['request_uri']); if($request->server['request_uri'] != "/favicon.ico"){ $headers = [ 'Content-Type'=>"text/html; charset=utf-8" ]; $this->setHeader($headers,$response); $response->end("<h1>hello swoole ".rand(100,999)."</h1>"); } } public function setHeader(array $headers ,$response) { foreach($headers as $key=>$header){ $response->header($key,$header); * / * Start / ** } } Start function public () { $ this-> http_serv-> Start (); } public function enableStaticHandel ($ DOCUMENT_ROOT) { $ this-> http_serv-> SET ( [ 'DOCUMENT_ROOT' => $ DOCUMENT_ROOT, the following version V4.4.0 // here must be an absolute path 'enable_static_handler' => to true, ] ); } } $ the HttpServer new new HTTP = (); $ http-> Start ();
cli execution
php HttpServer.php
Browser access
http://192.168.1.200:9501 ip ip for their own virtual machine