Thrift PHP example 示例

发布于 2023-09-28 07:31:27 字数 3731 浏览 36 评论 0

Thrift PHP example

app.thrift:

namespace php Thrift.Service

service Auth {
    string hello(1: string name)
}

其中 Thrift 包通过 composer 安装, gen-php 使用 classmapfiles 引入依赖,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

谢绝鈎搭

暂无简介

文章
评论
903 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文