Swoole based Simple HTTP Server Implementation using OSS PHP SDK

Image for post
Image for post

Introduction

Swoole, in the recent times, has taken over as the new “best language” in the coder’s world.

Advantages of Swoole

Let us examine its advantages.

Application Scenario

To understand Swoole more comprehensively, let us look at a use-case for Swoole.

1. Install Swoole

As the first step, you need to install Swoole on your system. If you have PHP7 and PECL installed, you can use the following command directly.
pecl install swoole

Image for post
Image for post

2. Use OSS PHP SDK

For using Swoole in OSS PHP SDK, you need to ensure that the PHP SDK setup is successful and ready. The steps are as follows:

Image for post
Image for post

3. Set Nginx for Reverse Proxy

After carrying out the steps mentioned above, you can access OSS through Swoole. However, the more common practice, on the server side, is to use nginx as a front-end proxy. Then access the initiated Swoole server through the nginx reverse proxy server.

Image for post
Image for post
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream swoole{
server 127.0.0.1:9503;
keepalive 4;
}
server {
listen 80;
server_name www.swoole.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://swoole;
proxy_set_header Connection "";
proxy_http_version 1.1;
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

4. Experiences using Swoole

If “Hello Swoole” is the only result after following the steps until now, then you are missing something.

<?php
require_once __DIR__ . '/Common.php';
use OSS\OssClient;
use OSS\Core\OssException;
$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
//*******************************Simple use***************************************************************
$options = array(
OssClient::OSS_FILE_DOWNLOAD => "example_download.jpg",
);
$ossClient->uploadFile($bucket, "example.jpg", "example.jpg");
$ossClient->getObject($bucket, "example.jpg", $options);
Image for post
Image for post
<?php
require_once __DIR__ . '/Common.php';
use OSS\OssClient;
use OSS\Core\OssException;
$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
//*******************************Simple use***************************************************************
$options = array(
OssClient::OSS_FILE_DOWNLOAD => "example_download.jpg",
);
$serv = new swoole_http_server("127.0.0.1", 9503);$serv->set(array(
'worker_num' => 16,
'daemonize' => true,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1,
'log_file' => '/tmp/swoole_http_server.log',
));
$serv->on('Request', function($request, $response) use($ossClient, $bucket, $options){ $ossClient->uploadFile($bucket, "example.jpg", "example.jpg");
$ossClient->getObject($bucket, "example.jpg", $options);
$response->end("Hello Swoole\n");
});
$serv->start();
Image for post
Image for post

Conclusion

The event-based network layer in Swoole takes advantage of the underlying epoll/kqueue implementation to cater to several thousand connections. After a request, the allocated memory does not free itself as it does in legacy apache/php-fpm, improving its performance significantly. You can create enhanced web applications with more control, real-time chatting servers, and much more with Swoole.

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store