LAMPlights Personal anecdotes from my experiences using the LAMP stack

16Jan/103

Facebook Thrift PHP Client

A while back I wrote a post about using Facebook's Thrift.  One comment asked me to post the PHP client used to connect to the C++ server I was demo'ing.  Most of the client is boiler-plate code generated by Thrift, so I chose to omit it at the time.  Here it is:

#!/usr/bin/env php
<?php
$GLOBALS['THRIFT_ROOT'] = '/path/to/thrift';
 
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 'Sale.php';
 
try {
  $socket = new TSocket('localhost', 9090);
  $transport = new TBufferedTransport($socket, 1024, 1024);
  $protocol = new TBinaryProtocol($transport);
  $client = new SaleClient($protocol);
 
  $transport->open();
 
  $d = new Sale_deal();
 
  $d->dealno = 12345;
  $d->term = 2;
  $d->payment = 500.1;
  $d->amtfin = 1000.00;
 
  echo 'Total cost: ' . $client->calc_apr($d) . "\n";
 
  $transport->close();
 
} catch (TException $tx) {
  print 'TException: '.$tx->getMessage()."\n";
}
Comments (3) Trackbacks (0)
  1. very useful thanks. i’m trying to put the finishing touches to an initial scribe deployment. sample client code is very useful.

    thanks

  2. What are the contents of Sale.php? Is that completely generated by Thrift?

    Since that is the definition of:
    $client = new SaleClient($protocol);

    it seems strange not to include it.

  3. The Sale.php file is generated from the Sale.thrift file. That file is shown in my previous post: http://www.hermanradtke.com/blog/exploring-facebooks-thrift/

    With the Sale.thrift file, I generated a php client and a C++ server. The above example was just showing how I used the Sale.php client code.
    Twitter:


Leave a comment


No trackbacks yet.