merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / demo / client / proxy.php
1 <?php require_once __DIR__ . "/_prepend.php"; ?><html lang="en">
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 <p>You can see the source to this page here: <a href="proxy.php?showSource=1">proxy.php</a></p>
8 <?php
9
10 class PhpXmlRpcProxy
11 {
12     protected $client;
13     protected $prefix = 'examples.';
14
15     public function __construct(PhpXmlRpc\Client $client)
16     {
17         $this->client = $client;
18     }
19
20     /**
21      * Translates any method call to an xmlrpc call.
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     public 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->faultString(), $resp->faultCode());
47         } else {
48             return $resp->value();
49         }
50     }
51 }
52
53 $stateNo = rand(1, 51);
54 $proxy = new PhpXmlRpcProxy(new PhpXmlRpc\Client(XMLRPCSERVER));
55 $stateName = $proxy->getStateName($stateNo);
56
57 echo "State $stateNo is ".htmlspecialchars($stateName);
58
59 require_once __DIR__ . "/_append.php";