LAMPlights Personal anecdotes from my experiences using the LAMP stack

16Jan/107

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";
}
  • andrew

    very useful thanks. i’m trying to put the finishing touches to an initial scribe deployment. sample client code is very useful.

    thanks

  • http://www.imagehawk.com Mark

    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.

  • Herman Radtke

    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.

  • http://twitter.com/dxdinamic Nikola Petkanski

    It is worth mentioning that the Sale_deal class is a VO (http://en.wikipedia.org/wiki/Data_transfer_object) and not a real object.

  • Ed

    Where is the definition of SaleClient?
    I’m confused as to how this is referenced anywhere!
    Thanks

  • Herman Radtke

    The SaleClient definition is in Sale.php. It is generated by Thrift, and should conform to the interface described in the .thrift file. The .thrift file definition is discussed in the previous post on thrift: http://www.hermanradtke.com/blog/?p=52

  • Mladenpetkanski

    Гледам че ме търсиш от време на време. В ФБ не ми пиши ,че е голяма клюкарница.Има все разни дето се бутат и те не знаят защо.Интересно оформление си си направил.От БЛОГА ти не схващам много,но ще го разгледам като имам време.Моят е-маил е  mladenpetkanski@abv.bg  Иначе нищо ново при нас.Записали се пак да учиш?Как си? Извиняваи за ФБ че не пиша.Но стават много интриги,а аз не си падам по шеги.Защо ми изпращаш сигнализиращ е-маил. Нали съм ти пращал е-маил-а си.Написал си всичко на англииски.Трудно ми е да го разбера,а сигорно и на много други.  До скоро! Младен