[Introduction to swoole] How to quickly create a web server

little bottle
Release: 2023-04-06 07:20:02
forward
1939 people have browsed it

Swoole is a PHP advanced web development framework that can improve website development efficiency. In this article, the editor will introduce how to use swoole to create a web server. Friends who are interested can learn it.

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.1Local 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 starting the service successfully, 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 http://127.0.0.1:9501?param=1<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

Related tutorials:

PHP video tutorial

Swoole interpretation Video tutorial

The above is the detailed content of [Introduction to swoole] How to quickly create a web server. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template