In this post, I wanted to demonstrate how to use Apache Thrift to create a simple client in PHP. This might be useful if you wanted to integrate the Workflow engine with a web page.
The first step is to generate the PHP bindings for the WEE Thrift service discussed previously. From the command line, run the following example:
thrift-0.8.0.exe -r --gen php wee.thrift
Second, create a directory where your web server can process a PHP file. In my case, I'm using Apache on a Windows computer so I create a directory named C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\example
Third, copy the PHP sample code shipped with the Apache Thrift distribution into the directory created above. In my case, the sample code is located in C:\thrift-0.8.0\lib\php\src and I copy this to C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\example\src
Four, take the code that was generated in Step 1 and copy this to the packages directory. the complete path in my example is C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\example\src\packages.
The following screen shot shows how the directory structures and files should appear.
Five, create a new file named wee_client.php. This file should be located in the C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\example directory. Copy the following code and paste it into the file.
<?php
$GLOBALS['THRIFT_ROOT'] = 'src';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/packages/wee/Wee.php';
try {
$socket = new TSocket('localhost', 9090);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new WeeClient($protocol);
$transport->open();
// create a new WEE Rule structure
$r = new Rule();
$r->filename = 'c:/temp/wee2.txt';
// Run the WEE rule
if ($client->run($r))
{
echo 'Rule ran succesfully</br>';
} else {
echo 'Rule did not run succesfully</br>';
}
} catch (TException $tx) {
print 'Thrift Exception: '.$tx->getMessage()."\n";
}
?>
$GLOBALS['THRIFT_ROOT'] = 'src';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/packages/wee/Wee.php';
try {
$socket = new TSocket('localhost', 9090);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new WeeClient($protocol);
$transport->open();
// create a new WEE Rule structure
$r = new Rule();
$r->filename = 'c:/temp/wee2.txt';
// Run the WEE rule
if ($client->run($r))
{
echo 'Rule ran succesfully</br>';
} else {
echo 'Rule did not run succesfully</br>';
}
} catch (TException $tx) {
print 'Thrift Exception: '.$tx->getMessage()."\n";
}
?>
I have highlighted in yellow the code that is specific to my example. Normally, you would replace this highlighted code with the names appropriate to your Apache Thrift service definition.
To run the PHP client, I open the URL http://localhost:8081/example/wee_client.php in my browser which displays the message Rule ran succesfully.
As you can see, most of the code that is needed is already supplied in the Apache Thrift distribution. You just need to copy this code and revise as needed.
No comments:
Post a Comment