Thrift PHP example 示例
Thrift PHP example
app.thrift:
namespace php Thrift.Service
service Auth {
string hello(1: string name)
}
其中 Thrift
包通过 composer 安装, gen-php
使用 classmap
或 files
引入依赖,transport
有多种方式选择,但是服务端应与客户端一致
建议使用 Socket 传输,不建议服务端使用 php 来构建
Thrift 手册: http://diwakergupta.github.io/thrift-missing-guide/
Server.php
<?php error_reporting(E_ALL); require 'vendor/autoload.php'; if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr"); } use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TPhpStream; use Thrift\Transport\TBufferedTransport; class Auth implements \Thrift\Service\AuthIf { /** * @param string $name * @return string */ public function hello($name) { return "Hello $name"; } } header('Content-Type', 'application/x-thrift'); if (php_sapi_name() == 'cli') { echo "\r\n"; } $handler = new Auth(); $processor = new \Thrift\Service\AuthProcessor($handler); $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); $protocol = new TBinaryProtocol($transport, true, true); $transport->open(); $processor->process($protocol, $protocol); $transport->close();
Client.php
<?php error_reporting(E_ALL); require 'vendor/autoload.php'; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TPhpStream; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Exception\TException; $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); $protocol = new TBinaryProtocol($transport); $client = new Thrift\Service\AuthClient($protocol); $transport->open(); $recv = $client->hello('kevin'); print $recv; $transport->close();
使用 socket
Server.php
<?php error_reporting(E_ALL); require 'vendor/autoload.php'; use Thrift\Factory\TBinaryProtocolFactory; use Thrift\Factory\TTransportFactory; if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr"); } use Thrift\Protocol\TBinaryProtocol; use Thrift\Server\TServerSocket; use Thrift\Server\TSimpleServer; use Thrift\Transport\TPhpStream; use Thrift\Transport\TBufferedTransport; class Auth implements \Thrift\Service\AuthIf { /** * @param string $name * @return string */ public function hello($name) { return "Hello $name"; } } header('Content-Type', 'application/x-thrift'); if (php_sapi_name() == 'cli') { echo "\r\n"; } $handler = new Auth(); $processor = new \Thrift\Service\AuthProcessor($handler); $transportFactory = new TTransportFactory(); $protocolFactory = new TBinaryProtocolFactory(true, true); $transport = new TServerSocket('localhost', 9090); $server = new TSimpleServer($processor, $transport, $transportFactory, $transportFactory, $protocolFactory, $protocolFactory); $server->serve();
Client.php
<?php error_reporting(E_ALL); require 'vendor/autoload.php'; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TPhpStream; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Exception\TException; $transport = new TBufferedTransport(new TSocket('localhost', 9090)); $protocol = new TBinaryProtocol($transport); $client = new Thrift\Service\AuthClient($protocol); $transport->open(); $recv = $client->hello('kevin'); print $recv; $transport->close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论