Swoole learning network communication engine Web Service (4)

Time:2019-12-9

I. HTTP service

HTTP server

Let’s look at the introduction guide to swoole official documents – > fast start – > create a web server, run the sample code of the document once, and see the following effect:

http_server.php

<?php

$http = new Swoole\Http\Server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);

    //Cookie test
    // $response->cookie('name', 'lily', time()+3600);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Open a window to access the service:

[email protected]:~# curl http://127.0.0.1:9501
<h1>Hello Swoole. #9147</h1>[email protected]:~# curl http://127.0.0.1:9501?act=all
<h1>Hello Swoole. #4674</h1>[email protected]:~#

The HTTP server only needs to pay attention to the request response, so it only needs to listen to an onrequest event. This event is triggered when a new HTTP request enters. The event callback function has two parameters, one is the $request object, which contains the relevant information of the request, such as the data of get / post request.

The other is the response object. The response to the request can be completed by operating the response object. The $response – > end() method means to output a piece of HTML content and end the request.

  • 0.0.0.0 means listening to 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 local area network IP, 210.127.20.2 external network IP. Here you can also specify a single IP to listen to
  • 9501 listening port, if the occupied program will throw a fatal error, interrupt execution.

Static content

When static pages such as test.html do not follow the PHP logic, we need to do special configuration here

<?php

$http = new Swoole\Http\Server("0.0.0.0", 9501);

//Static resource settings. If related resources are found, they will be returned to the browser directly, and the following logic will not be followed (like nginx)
$http->set([
    'enable_static_handle' => true,
    'document_root' = > "/ work / study / code / swoole / static" // store static resource path
]);

$http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);

    //Cookie test
    // $response->cookie('name', 'lily', time()+3600);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Recommended Today

Docker learning (5) basic command of dockerfile

To learn dockerfile, you need to understand its basic commands From – base image Try to use the official reference image [x] From Tomcat: 8.5.50-jdk8-openjdk 񖓿 make reference image (based on Tomcat: 8.5.50-jdk8-openjdk) [x] From CentOS ා make a base image based on CentOS: latest [x] From scratch? Does not depend on any reference image […]