thrift php api composer package
PHP C++
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/Thrift
src
test udpate to thrift0.9.1 Oct 21, 2014
.gitignore
Makefile.am
Makefile.in udpate to thrift0.9.1 Oct 21, 2014
README udpate to thrift0.9.1 Oct 21, 2014
README.apache udpate to thrift0.9.1 Oct 21, 2014
README.md
composer.json fix package homepage Oct 17, 2012
thrift_protocol.ini

README.md

php-thrift-package

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.