Install the PHP extension for swoole

Time:2021-8-17

Installation preparation

Before installing swoole, you must ensure that the following software has been installed on the system

reference resources

https://github.com/LinkedDestiny

Php-5.3.10 // or later
Gcc-4.4 // or later
make
autoconf

Install the PHP extension for swoole

wget https://github.com/swoole/swoole-src/archive/swoole-1.8.2-stable.tar.gz
tar -zxvf swoole-1.8.2-stable
cd swoole-src-swoole-1.8.2-stable/
/usr/local/php/bin/phpize
./configure
make && make install

Configure php.ini

After successful compilation and installation, modify php.ini to add

extension=swoole.so

test

Server side (server. PHP)

<?php
/**
 *Server
 */
$serv = new swoole_server("0.0.0.0", 9501);
$serv->on('connect', function ($serv, $fd){
    echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, 'Swoole: '.$data);
});
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});
$serv->start();

Client (client. PHP)

<?php
    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    $client->on("connect", function($cli) {
        $cli->send("hello world\n");
    });
    $client->on("receive", function($cli, $data){
        echo "Receive: $data\n";
    });
    $client->on("error", function($cli){
        echo "connect fail\n";
    });
    $client->on("close", function($cli){
        echo "close\n";
    });
    $client->connect('127.0.0.1', 9501, 0.5);

test

Run the server first (no output)

Install the PHP extension for swoole

After running the client, if you check the server, you will find that there is output, indicating success ~!

Install the PHP extension for swoole