惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy International News Feed
A
Arctic Wolf
Security Latest
Security Latest
雷峰网
雷峰网
V2EX - 技术
V2EX - 技术
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Schneier on Security
WordPress大学
WordPress大学
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
Scott Helme
Scott Helme
爱范儿
爱范儿
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Application and Cybersecurity Blog
Application and Cybersecurity Blog
I
Intezer
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
L
LINUX DO - 最新话题
W
WeLiveSecurity
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
IT之家
IT之家
N
News and Events Feed by Topic
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
小众软件
小众软件
博客园 - 叶小钗
Latest news
Latest news
P
Proofpoint News Feed
G
GRAHAM CLULEY
Schneier on Security
Schneier on Security
T
Tor Project blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Project Zero
Project Zero
The Cloudflare Blog
美团技术团队
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
Jina AI
Jina AI
V
Visual Studio Blog
Help Net Security
Help Net Security

博客园 - qufo

Golang 线程池 Docker 实现的 redis 主从 Golang 之 Base62 编码 Golang 之 Qrcode 二维码 Golang 之 key-value LevelDB [转] putty 使用密钥登陆 OpenSSH 一个视图引发的血案 破一个行业ERP的感想 Practical Ext JS Projects with Gears中关于Gears描述。 七七前一天,搞定 PHP5 + Oracle 8.1.7 去掉PowerDesigner 15 在 Visual Studio 2008里的不兼容。 回家 庆祝自己通过驾驶员考试 2008.08.08.一个有记住意义的时刻。 adverbux.com 明天路考 blank.security 距离2008年8月8日还有8天。 随时10个可用线程--自己涂鸦的“线程池”
无废话 Thrift 之 Hello World( PHP 版).
qufo · 2016-06-22 · via 博客园 - qufo

Thrift 不再介绍。体验一把 PHP 的Server , PHP 的Client。

0.安装,装备环境,不表,运行 thrift -version 看到版本就行。

1.写 HelloThrift.thrift .

这个是个标准文本,里面只有一个sayHello 方法。

1 namespace php HelloThrift
2 
3 service HelloService {
4     string sayHello(1:string username)
5 }

通过 thrift -r --gen php:server HelloThrift.thrift 命令会在当前目录下生成一个  gen-php 。为了方便改名为 gen_php 。注意划线。

2.把 thrift 的 lib文件夹 拷到项目的根下

3.写服务端。Server.php

<?php
namespace HelloThrift\php;

error_reporting(E_ALL);

require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;

$GEN_DIR = realpath(dirname(__FILE__)).'/gen_php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib');
$loader->registerDefinition('HelloThrift',$GEN_DIR);
$loader->register();

if (php_sapi_name() == 'cli') {
    ini_set('display_errors',"stderr");
}

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;

class HelloHandler implements \HelloThrift\HelloServiceIf {

    public function sayHello($username)
    {
        return "Hello ".$username;
    }
}

header('Content-Type','application/x-thrift');
if (php_sapi_name() == 'cli') {
    echo PHP_EOL;
}

$handler = new HelloHandler();
$processor = new \HelloThrift\HelloServiceProcessor($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();

服务器的只有一个sayHello 方法。在require 时注意一下文件路径。

4.写客户端。Client.php

<?php
namespace  HelloThrift\php;

error_reporting(E_ALL);
require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;

$GEN_DIR = realpath(dirname(__FILE__)).'/gen_php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib');
$loader->registerDefinition('HelloThrift',$GEN_DIR);
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;

try {
    if (array_search('--http',$argv)) {
        $socket = new THttpClient('localhost',8080,'/Server.php');
    } else {
        $socket = new TSocket('localhost',9090);
    }

    $transport = new TBufferedTransport($socket,1024,1024);
    $protocol  = new TBinaryProtocol($transport);
    $client = new \HelloThrift\HelloServiceClient($protocol);

    $transport->open();

    echo $client->sayHello(" World! ");

    $transport->close();
} catch (\Exception $e) {
    print 'TException:'.$e->getMessage().PHP_EOL;
}

里面也只调用一次 sayHello 方法。

5.运行服务。在项目的根下运行命令  php -S localhost:8080

6.运行客户端进行测试。直接使用 php Client --http 命令。可以看到 Hello World! 成功。

7.文件夹结构如下:

8: 整个项目文件打包。

    好象我没有上传权限,算了,自己写的文件代码全在上面了。其他的是 thrift 自己生成的。