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)

不言
Release: 2023-04-04 19:24:01
forward
2672 people have browsed it

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();
Copy after login

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
Copy after login

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  
Copy after login

3. Simulate http request

$ sudo curl  
<h1>Hello Swoole.#1061</h1>
Copy after login

The server prints get/post request data

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

4. End the process

kill 952
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template