PHP implements simple HTTP server reference based on Swoole official definition of Swoole:
The PHP asynchronous, parallel, and high-performance network communication framework, written in pure C language, provides PHP asynchronous multi-thread server, asynchronous TCP/UDP network client, asynchronous MySQL, database connection pool, asyncTask, message queue, millisecond timer, asynchronous file read/write, asynchronous DNS query. Swoole can be widely used in the Internet, mobile communication, enterprise software, cloud computing, online games, IoT, IoV, smart home, and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of enterprise it r & D teams and focus more on developing innovative products.
The following describes how to write simple HTTP servers using swoole APIs.
Swoole_server
Use the swoole_server API to build an HTTP server in four steps:
- Build Server objects
- Set runtime parameters
- Register the event callback function
- Start the server
Direct code embodiment, newServer. php:
Set (array ('worker _ num' => 8, 'daemonize '=> 0, 'max _ request' => 10000, 'Dispatch _ mode' => 2, 'debug _ mode' => 1,); // 3. register the event callback function $ serv-> on ('receive ', function ($ serv, $ fd, $ from_id, $ data) {$ respData = 'Hello Swoole. '; response ($ serv, $ fd, $ respData); // encapsulate and send HTTP response packets}); // 4. start the server $ serv-> start ();
How to encapsulate HTTP response packets? First, you need to know the composition structure of the HTTP response packet, such
Knowing the structure of the response message, my response message should be like this:
HTTP/1.1 200Server:SwooleServerContent-Type:text/html;charset=utf8Content-Length:13Hello Swoole.
Code implementation.
/*** Sent content ** @ param \ swoole_server $ serv * @ param int $ fd * @ param string $ respData * @ return void */function response ($ serv, $ fd, $ respData) {// response line $ response = array ('http/1.1 200 ',); // response header $ headers = array ('server' => 'swooleserver ', 'content-type' => 'text/html; charset = utf8', 'Content-length' => strlen ($ respData ),); foreach ($ headers as $ key => $ val) {$ response [] = $ key. ':'. $ val;} // empty row $ response [] = ''; // response body $ response [] = $ respData; $ send_data = join (" \ r \ n ", $ response); $ serv-> send ($ fd, $ send_data );}
At this point, a simple responseHello Swoole.The HTTP server is complete. Complete Code: here
(Cannot access gist? Configure the local hosts file: 192.30.252.141 gist.github.com)
Run php server. php, the browser accesses http: // 127.0.0.1: 9501/. of course, we often use Nginx as the front-end proxy, set a www.server.com, set the hosts ing locally, and access through the domain name. Nginx configuration: here
Note: When the response message format is incorrect, the browser page will always be switched to chrysanthemum, waiting for the request to return...
Later, with swoole_http_server, the HTTP server code became simpler!
Swoole_http_server
on('request', function ($request, $response) { $html = "Hello Swoole."; $response->end($html);});
That's right, just a few lines of code. Response packets no longer need to be encapsulated. Swoole_http_server is inherited from swoole_server and is supported by swoole's built-in Http server. with several lines of code, you can write an Http server that is asynchronous, non-blocking, and multi-process.
Notice: swoole_http_server does not fully support Http. we recommend that you use it as the application server only. Add Nginx as a proxy at the front end.
Reference
- Http://www.swoole.com/
- Https://github.com/LinkedDestiny/swoole-doc