Home >Backend Development >PHP Tutorial >Introduction to swoole's method of creating a web server (code example)

Introduction to swoole's method of creating a web server (code example)

不言
不言forward
2019-01-18 10:04:092682browse

This article brings you an introduction to the method of creating a web server with swoole (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

http_server.php

$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 means monitoring all IP addresses. A server may have multiple IPs at the same time, such as 127.0.0.1 local loopback IP, 192.168.1.100 LAN IP, 210.127.20.2 external network IP, you can also specify a separate IP to monitor here.

1. Start the service

$ /usr/local/php/bin/php http_server.php

2. After the service is successfully started, check netstat

$ 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. Simulate http request

$ sudo curl  
4a249f0d628e2318394fd9b75b4636b1Hello Swoole.#1061473f0a7621bec819994bb5020d29372a

The server prints get/post request data

$ /usr/local/php/bin/php http_server.php 
array(1) {
  ["param"]=>
    string(1) "1"
    }
NULL

4. End the process

kill 952

The above is the detailed content of Introduction to swoole's method of creating a web server (code example). For more information, please follow other related articles on the PHP Chinese website!

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