blob: ab38c1111170356167e6385d85cf483baeee09f3 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env php
2<?php
3
4$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
5
6require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
7require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
8require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
David Reiss755c8152009-03-26 06:15:05 +00009require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
Mark Slee07a3aab2007-03-07 05:45:10 +000010require_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 */
20error_reporting(E_NONE);
21$GEN_DIR = '../gen-php';
22require_once $GEN_DIR.'/SharedService.php';
23require_once $GEN_DIR.'/shared_types.php';
24require_once $GEN_DIR.'/Calculator.php';
25require_once $GEN_DIR.'/tutorial_types.php';
26error_reporting(E_ALL);
27
Mark Slee07a3aab2007-03-07 05:45:10 +000028try {
David Reiss755c8152009-03-26 06:15:05 +000029 if (array_search('--http', $argv)) {
30 $socket = new THttpClient('localhost', 8080, '/php/PhpServer.php');
31 } else {
32 $socket = new TSocket('localhost', 9090);
33 }
Mark Slee76791962007-03-14 02:47:35 +000034 $transport = new TBufferedTransport($socket, 1024, 1024);
35 $protocol = new TBinaryProtocol($transport);
36 $client = new CalculatorClient($protocol);
David Reiss0c90f6f2008-02-06 22:18:40 +000037
Mark Slee76791962007-03-14 02:47:35 +000038 $transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000039
Mark Slee76791962007-03-14 02:47:35 +000040 $client->ping();
41 print "ping()\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000042
Mark Slee76791962007-03-14 02:47:35 +000043 $sum = $client->add(1,1);
44 print "1+1=$sum\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000045
Mark Slee76791962007-03-14 02:47:35 +000046 $work = new tutorial_Work();
David Reiss0c90f6f2008-02-06 22:18:40 +000047
Mark Slee76791962007-03-14 02:47:35 +000048 $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 Reiss0c90f6f2008-02-06 22:18:40 +000058
Mark Slee76791962007-03-14 02:47:35 +000059 $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 Reiss0c90f6f2008-02-06 22:18:40 +000064
Mark Slee76791962007-03-14 02:47:35 +000065 $log = $client->getStruct(1);
66 print "Log: $log->value\n";
David Reiss0c90f6f2008-02-06 22:18:40 +000067
Mark Slee76791962007-03-14 02:47:35 +000068 $transport->close();
David Reiss0c90f6f2008-02-06 22:18:40 +000069
Mark Slee76791962007-03-14 02:47:35 +000070} catch (TException $tx) {
71 print 'TException: '.$tx->getMessage()."\n";
Mark Slee07a3aab2007-03-07 05:45:10 +000072}
73
Mark Slee07a3aab2007-03-07 05:45:10 +000074?>