Refactor demo files (client side): remove redundant ones; make sure all use existing...
[plcapi.git] / demo / client / proxy.php
1 <html>
2 <head><title>xmlrpc - Proxy demo</title></head>
3 <body>
4 <h1>proxy demo</h1>
5 <h2>Query server using a 'proxy' object</h2>
6 <h3>The code demonstrates usage for the terminally lazy</h3>
7 <?php
8
9 include_once __DIR__ . "/../../src/Autoloader.php";
10 PhpXmlRpc\Autoloader::register();
11
12 class PhpXmlRpcProxy
13 {
14     protected $client;
15     protected $prefix = 'examples.';
16
17     public function __construct(PhpXmlRpc\Client $client)
18     {
19         $this->client = $client;
20     }
21
22     /**
23      * @author Toth Istvan
24      *
25      * @param string $name remote function name. Will be prefixed
26      * @param array $arguments
27      *
28      * @return mixed
29      *
30      * @throws Exception
31      */
32     function __call($name, $arguments)
33     {
34         $encoder = new PhpXmlRpc\Encoder();
35         $valueArray = array();
36         foreach ($arguments as $parameter) {
37             $valueArray[] = $encoder->encode($parameter);
38         }
39
40         // just in case this was set to something else
41         $this->client->return_type = 'phpvals';
42
43         $resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $valueArray));
44
45         if ($resp->faultCode()) {
46             throw new Exception($resp->faultMessage(), $resp->faultCode);
47         } else {
48             return $resp->value();
49         }
50     }
51
52 }
53
54 $stateNo = rand(1, 51);
55 $proxy = new PhpXmlRpcProxy(new \PhpXmlRpc\Client('http://phpxmlrpc.sourceforge.net/server.php'));
56 $stateName = $proxy->getStateName($stateNo);
57
58 echo "State $stateNo is ".htmlspecialchars($stateName);