swoole HttpServerを学びます

ファイルHttpServer.php

<?phpの
/ ** 
 * PhpStormによって作成されます。
 *ユーザー:マック
 *発売日:2019年9月13日
 *時間:21時
 * / 

クラスHttpServerを
{ 
	公共$ http_serv。

	constのPORT = 9501; 
	constのIPは= "0.0.0.0"。

	公共の$ static_handel_switch =はtrue。
	公共$ DOCUMENT_ROOT = '/ www /のswoole'; 

	パブリック関数の__construct()
	{ 
		の$ this - > http_serv =新しいSwoole \のHttp \サーバー(自己:: IP、自己:: PORT)。

		の$ this - > http_serv->上( "要求"、[$この、 "onRequest"]); 

		もし(の$ this - > static_handel_switch ==真){ 
			の$ this - > enableStaticHandel(の$ this - > DOCUMENT_ROOT); 
		} 
	} 


	/ ** 
	 *接受到客户端请求 
	 * @paramの$応答响应
	 * / 
	パブリック関数onRequest($要求、$応答)
	{ 


		//のvar_dump($要求- >サーバ[ 'REQUEST_URI'])。
		もし(!$要求- >サーバー[ 'REQUEST_URI'] = "/favicon.ico"){ 


			$ヘッダ= [ 
				'Content-Typeの' => "text / htmlの;のcharset = UTF-8" 
			]; 

			の$ this - >のsetHeader($ヘッダ、$応答); 

			$対応- >終了( "<H1>こんにちはswoole" .rand(100999) "</ H1>"); 
		} 


	} 

	パブリック関数のsetHeader(配列$ヘッダ、$応答)
	{ 
		foreachの($キー=> $ヘッダとして$ヘッダ){ 
			$対応- >ヘッダ($キー、$ヘッダ)。
 
	パブリック関数の開始()
	{ 
		の$ this - > http_serv->開始(); 
	}
 
	パブリック関数enableStaticHandel($ DOCUMENT_ROOT)
	{ 
		の$ this - > http_serv-> SET(
			[ 
				=> $ DOCUMENT_ROOT、次のバージョンV4.4.0 // 'DOCUMENT_ROOT'をここでは、絶対パスでなければなりません
				、trueに> 'enable_static_handler' = 
			] 
		); 
	} 
} 

; =()HttpServerを新しい新しいHTTP $ 
$ HTTP->スタート()を。

CLIの実行

PHP HttpServer.php

 

ブラウザアクセス

http://192.168.1.200:9501 IP、IP、自分の仮想マシンの

 

おすすめ

転載: www.cnblogs.com/php-linux/p/11517736.html