Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env php |
| 2 | <?php |
| 3 | |
| 4 | $GLOBALS['THRIFT_ROOT'] = '../../lib/php/src'; |
| 5 | |
| 6 | require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; |
| 7 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; |
| 8 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 9 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php'; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 10 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; |
| 11 | |
| 12 | /** |
| 13 | * Suppress errors in here, which happen because we have not installed into |
| 14 | * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to! |
| 15 | * |
| 16 | * Normally we would only have to include Calculator.php which would properly |
| 17 | * include the other files from their packages/ folder locations, but we |
| 18 | * include everything here due to the bogus path setup. |
| 19 | */ |
| 20 | error_reporting(E_NONE); |
| 21 | $GEN_DIR = '../gen-php'; |
| 22 | require_once $GEN_DIR.'/SharedService.php'; |
| 23 | require_once $GEN_DIR.'/shared_types.php'; |
| 24 | require_once $GEN_DIR.'/Calculator.php'; |
| 25 | require_once $GEN_DIR.'/tutorial_types.php'; |
| 26 | error_reporting(E_ALL); |
| 27 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 28 | try { |
David Reiss | 755c815 | 2009-03-26 06:15:05 +0000 | [diff] [blame] | 29 | if (array_search('--http', $argv)) { |
| 30 | $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); |
| 31 | } else { |
| 32 | $socket = new TSocket('localhost', 9090); |
| 33 | } |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 34 | $transport = new TBufferedTransport($socket, 1024, 1024); |
| 35 | $protocol = new TBinaryProtocol($transport); |
| 36 | $client = new CalculatorClient($protocol); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 37 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 38 | $transport->open(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 39 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 40 | $client->ping(); |
| 41 | print "ping()\n"; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 42 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 43 | $sum = $client->add(1,1); |
| 44 | print "1+1=$sum\n"; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 46 | $work = new tutorial_Work(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 47 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 48 | $work->op = tutorial_Operation::DIVIDE; |
| 49 | $work->num1 = 1; |
| 50 | $work->num2 = 0; |
| 51 | |
| 52 | try { |
| 53 | $client->calculate(1, $work); |
| 54 | print "Whoa! We can divide by zero?\n"; |
| 55 | } catch (tutorial_InvalidOperation $io) { |
| 56 | print "InvalidOperation: $io->why\n"; |
| 57 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 58 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 59 | $work->op = tutorial_Operation::SUBTRACT; |
| 60 | $work->num1 = 15; |
| 61 | $work->num2 = 10; |
| 62 | $diff = $client->calculate(1, $work); |
| 63 | print "15-10=$diff\n"; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 64 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 65 | $log = $client->getStruct(1); |
| 66 | print "Log: $log->value\n"; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 68 | $transport->close(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 69 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 70 | } catch (TException $tx) { |
| 71 | print 'TException: '.$tx->getMessage()."\n"; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 74 | ?> |