Home  >  Article  >  Backend Development  >  [Introduction to swoole] How to quickly create a web server

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

little bottle
little bottleforward
2019-04-20 11:57:491938browse

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();

 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

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  

3. Simulate http request

$ sudo curl http://127.0.0.1:9501?param=1<h1>Hello Swoole.#1061</h1>

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

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!

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