leric/php-thrift

Thrift client api for PHP(version sync with thrift)

Installs: 50 973

Dependents: 1

Stars: 7

Watches: 1

Forks: 4

Language: PHP

0.9.1 2014-10-27 05:35 UTC

README

The repository just extracted PHP lib code from thrift code base to work better with composer. To install this lib, just add a requirement in composer.json:

"require": {
    "leric/php-thrift": "0.9.*" 
}

We'll take Hbase's thrift api as an example: [http://svn.apache.org/viewvc/hbase/trunk/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift?view=markup]

Thrift Client

Generate thrift client code:

/project/root$ thrift -gen php:oop Hbase.thrift
/project/root$ mv gen_php thrift

Setup composer to autoload generated code in thrift directory:

"autoload": {
    ... 
    "classmap": ["thrift/"]
}

Use thrift client:

<?php
try {

    $socket = new TSocket('localhost', 9090);
    $transport = new TBufferedTransport($socket);
    $protocol = new TBinaryProtocol($transport);

    $client = new HbaseClient($protocol);
    $transport->open();

    var_dump($client->getTableNames());
} catch (IOError $ex) {
    echo $ex->message;
}

better use a DI container to get rid of these boilerplate code.

Thrift Server

Generate thrift server code:

/project/root$ thrift -gen php:server API.thrift
/project/root$ mv gen_php thrift

Setup composer to autoload generated code in thrift directory:

"autoload": {
    ... 
    "classmap": ["thrift/"]
}

Setup a simple thrift server:

<?php

$handler = new HbaseServiceHandler();
$processor = new HbaseProcessor($handler);
$transport = new TServerSocket('localhost', 9090);
$server = new TSimpleServer($processor, $transport);
$server->serve();

The type HbaseServiceHandler is an implementation of HbaseIf interface, which contains all the business logic. HbaseProcessor is one of the classes generated by thrift in thrift/Hbase/Hbase.php.