service Calculator {
i32 add(1:i32 num1, 2:i32 num2)
}
使用 Thrift 编译器生成 PHP 代码:
bash
thrift gen php example.thrift
然后在 PHP 中使用:
php
require_once 'vendor/autoload.php';
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Example\CalculatorClient;
$socket = new TSocket('localhost', 9090);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new CalculatorClient($protocol);
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TServerSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Server\TSimpleServer;
class CalculatorHandler implements \Example\CalculatorIf {
public function add($num1, $num2) {
return $num1 + $num2;
}
}
$handler = new CalculatorHandler();
$processor = new \Example\CalculatorProcessor($handler);
$transport = new TServerSocket(localhost, 9090);
$protocol = new TBinaryProtocol($transport);