PHP code example of swoole-foundation / swoole-thrift

1. Go to this page and download the library: Download swoole-foundation/swoole-thrift library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swoole-foundation / swoole-thrift example snippets


    
    /**
     * thrift服务端
     * @author xialeistudio
     * @date 2019-05-16
     */
    
    use swoole\foundation\thrift\factory\TFramedTransportFactory;
    use swoole\foundation\thrift\server\SwooleServer;
    use swoole\foundation\thrift\server\SwooleServerTransport;
    use Swoole\Server;
    use tests\handler\SumServiceImpl;
    use tests\services\SumService\SumServiceProcessor;
    use Thrift\Exception\TTransportException;
    use Thrift\Factory\TBinaryProtocolFactory;
    
    
            printf("Server::serve on %s:%d\n", $server->host, $server->port);
        });
        $server->serve();
    } catch (TTransportException $e) {
        printf("Server::serve error: {$e->getMessage()}\n");
    }
    

    
    /**
     * thrift客户端,共用一个连接
     * @author xialeistudio
     * @date 2019-05-16
     */
    
    use swoole\foundation\thrift\client\Transport;
    use tests\services\SumService\SumServiceClient;
    use Thrift\Protocol\TBinaryProtocol;
    use Thrift\Transport\TFramedTransport;
    
    
        $start = microtime(true);
        $client->sum(1, 1);
        $duration = microtime(true) - $start;
        $max = max($duration, $max);
        $min = $min === 0 ? $duration : min($duration, $min);
        $count++;
        $total += $duration;
    }
    
    printf("max: %fs\nmin: %fs\navg: %fs\ncall count: %d\ntotal time: %fs\n", $max, $min, $total / $count, $count, $total);