Building API Interface Services using Swoole

Source: Internet
Author: User
Tags explode

There are a lot of similar articles on the Internet, and I'm just getting started. Learn from the beginning. So if you repeat the article to explain, instead of wasting time, so you build a demo, using Swoole TCP server to accept the TCP client's HTTP request, and then simply split the GET request, get the required parameters, and then return the parameters back to the TCP client. This example, to show that TCP is a transport layer, and we get the HTTP request, how to parse the sent text is the application layer, which is usually Apache or nginx to help us do a good job.

The following is all the code, because it is practice code, so the naming is not too canonical. I want to parse the POST request and get request, but after the practice found that the post request is more difficult to parse, just Swoole has a perfect swoole_http_server this tool, can help me to deal with some tedious preparation work, The following code is only used to show some of the process and understanding of Swoole, welcome friends to discuss together.

<?php/*** Build an API server with Swoole */$serv=NewSwoole_server(' 127.0.0.1 ', 9888, swoole_process, swoole_sock_tcp);$serv-Set(Array(   ' Worker_num '=4,   ' Daemonize '=false,   ' Backlog '= -,));$serv->on(' Connect ', ' OnConnect ');$serv->on(' Receive ', ' OnReceive ');$serv->on(' Close ', ' OnClose ');$serv->start();/***http establishing a connection callback function */functionOnConnect($serv, $FD){Echo "Server is running on 127.0.0.1:9888".Php_eol;}/*** Start accepting Client information callback function */functionOnReceive($serv, $FD, $from _id, $data){Formatrequest($serv, $FD, $data);}/*** Callback function after connection shutdown */functionOnClose($serv, $FD){return false;}/*** Package The contents of response */functionResponse($serv, $FD, $res _data=''){//Response line    $response=Array(        ' http/1.1 ',    );    //Response head    $headers=Array(        ' Server '=' Swooleserver ',        ' Content-type '=' Text/html;charset=utf8 ',        ' Content-length '=strlen($res _data),    );    foreach($headers  as $key=$val){$response[]=$key.':'.$val;}//Blank line    $response[]='';    //Response body    $response[]=$res _data;    $send _data=Join("\ r \ n",$response);    $serv->send($FD, $send _data);}functionFormatrequest($serv, $FD, $data){$tmp _data=Explode("\ r \ n", $data);    if (!Is_array($tmp _data)||Empty($tmp _data)) return false;    $request _info=Explode(" ", $tmp _data[0]);    if (!Is_array($request _info)||Empty($request _info)) return false;    Switch ($request _info[0]){ Case' GET ':            $tmp _str=UrlDecode($request _info[1]);            $query _info=Parse_url($tmp _str);            $query=$query _info[' query '];            $path=$query _info[' path '];            Global $_get;            Parse_str($query, $_get);Response($serv, $FD, Json_encode($_get));             Break;         Case' POST ':            //needs Some code             Break;        Default:            return false;             Break;}}functionUrlrouter(){}/** Register global variables Post parameters* Register global variables Get parameters */functionRegister_global_request_var($data, $type){return false;}

This article refers to:

    1. Swoole use
    2. Understanding the RESTful architecture
    3. PHP implementation based on Swoole simple HTTP Server

Building API Interface Services using Swoole

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.