swoole創(chuàng)建web服務(wù)器的方法介紹(代碼示例)
來源:不言
發(fā)布時間:2019-01-18 14:32:25
閱讀量:887
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于swoole創(chuàng)建web服務(wù)器的方法介紹(代碼示例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
http_server.php
1 2 3 4 5 6 7 8 | $http = new swoole_http_server( "0.0.0.0" , 9501);
$http ->on( 'request' , function ( $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>\n" );
});
$http ->start();
|
0.0.0.0 表示監(jiān)聽所有IP地址,一臺服務(wù)器可能同時有多個IP,如127.0.0.1本地回環(huán)IP、192.168.1.100局域網(wǎng)IP、210.127.20.2 外網(wǎng)IP,這里也可以單獨(dú)指定監(jiān)聽一個IP。
1、啟動服務(wù)
1 | $ /usr/local/php/bin/php http_server.php
|
2、啟動服務(wù)成功后,netstat查看
1 2 3 4 | $ ps aux | grep http_server
oosten 952 0.0 2.2 314544 23176 pts/3 Sl+ 14:17 0:00 /usr/local/php/bin/php http_server.php
oosten 953 0.0 0.4 240212 4132 pts/3 S+ 14:17 0:00 /usr/local/php/bin/php http_server.php
oosten 955 0.0 0.7 242620 7408 pts/3 S+ 14:17 0:00 /usr/local/php/bin/php http_server.php
|
3、模擬http請求
1 2 | $ sudo curl
<h1>Hello Swoole.#1061</h1>
|
服務(wù)端打印get/post請求數(shù)據(jù)
1 2 3 4 5 6 | $ /usr/local/php/bin/php http_server.php
array (1) {
[ "param" ]=>
string(1) "1"
}
NULL
|
4、結(jié)束進(jìn)程