Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / php / phpxmlrpc / 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. For a more complete proxy, look at at the Wrapper class</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      * Translates any method call to an xmlrpc call.
24      *
25      * @author Toth Istvan
26      *
27      * @param string $name remote function name. Will be prefixed
28      * @param array $arguments
29      *
30      * @return mixed
31      *
32      * @throws Exception
33      */
34     function __call($name, $arguments)
35     {
36         $encoder = new PhpXmlRpc\Encoder();
37         $valueArray = array();
38         foreach ($arguments as $parameter) {
39             $valueArray[] = $encoder->encode($parameter);
40         }
41
42         // just in case this was set to something else
43         $this->client->return_type = 'phpvals';
44
45         $resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $valueArray));
46
47         if ($resp->faultCode()) {
48             throw new Exception($resp->faultString(), $resp->faultCode());
49         } else {
50             return $resp->value();
51         }
52     }
53
54 }
55
56 $stateNo = rand(1, 51);
57 $proxy = new PhpXmlRpcProxy(new \PhpXmlRpc\Client('http://phpxmlrpc.sourceforge.net/server.php'));
58 $stateName = $proxy->getStateName($stateNo);
59
60 echo "State $stateNo is ".htmlspecialchars($stateName);