1
|
|
|
<?php |
2
|
|
|
require_once __DIR__ . "/_prepend.php"; |
3
|
|
|
|
4
|
1 |
|
output('<html lang="en"> |
5
|
|
|
<head><title>phpxmlrpc - Proxy demo</title></head> |
6
|
|
|
<body> |
7
|
|
|
<h1>proxy demo</h1> |
8
|
|
|
<h2>Query server using a "proxy" object</h2> |
9
|
|
|
<h3>The code demonstrates usage for the terminally lazy. For a more complete proxy, look at the Wrapper class</h3> |
10
|
|
|
<p>You can see the source to this page here: <a href="proxy.php?showSource=1">proxy.php</a></p> |
11
|
|
|
'); |
12
|
|
|
|
13
|
|
|
class XmlRpcProxy |
14
|
|
|
{ |
15
|
|
|
protected $client; |
16
|
|
|
protected $prefix; |
17
|
|
|
protected $encoder; |
18
|
1 |
|
protected $encodingOptions = array(); |
19
|
|
|
|
20
|
1 |
|
/** |
21
|
1 |
|
* We rely on injecting a fully-formed Client, so that all the necessary http/debugging options can be set into it |
22
|
1 |
|
* without the need for this class to reimplement support for that configuration. |
23
|
|
|
*/ |
24
|
|
|
public function __construct(PhpXmlRpc\Client $client, $prefix = 'examples.', $encodingOptions = array()) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
$this->prefix = $prefix; |
28
|
|
|
$this->encodingOptions = $encodingOptions; |
29
|
|
|
$this->encoder = new PhpXmlRpc\Encoder(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Translates any php method call to an xml-rpc call. |
34
|
|
|
* Note that the server might expose methods which can not be called directly this way, because their name includes |
35
|
|
|
* characters which are not allowed in a php method. That's why we implement as well method `call` |
36
|
1 |
|
* |
37
|
|
|
* @author Toth Istvan |
38
|
1 |
|
* |
39
|
1 |
|
* @param string $name remote function name. Will be prefixed |
40
|
1 |
|
* @param array $arguments any php value will do. For xml-rpc base64 values, wrap them in a Value object |
41
|
1 |
|
* @return mixed |
42
|
|
|
* |
43
|
|
|
* @throws Exception |
44
|
|
|
*/ |
45
|
1 |
|
public function __call($name, $arguments) |
46
|
|
|
{ |
47
|
1 |
|
$args = array(); |
48
|
|
|
foreach ($arguments as $parameter) { |
49
|
1 |
|
$args[] = $this->encoder->encode($parameter, $this->encodingOptions); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
// just in case this was set to something else |
53
|
|
|
$originalReturnType = $this->client->getOption(\PhpXmlRpc\Client::OPT_RETURN_TYPE); |
54
|
|
|
$this->client->setOption(\PhpXmlRpc\Client::OPT_RETURN_TYPE, 'phpvals'); |
55
|
|
|
|
56
|
|
|
$resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $args)); |
57
|
|
|
|
58
|
|
|
$this->client->setOption(\PhpXmlRpc\Client::OPT_RETURN_TYPE, $originalReturnType); |
59
|
|
|
|
60
|
|
|
if ($resp->faultCode()) { |
61
|
|
|
throw new \Exception($resp->faultString(), $resp->faultCode()); |
62
|
|
|
} else { |
63
|
|
|
return $resp->value(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* In case the remote method name has characters which are not valid as php method names, use this. |
69
|
|
|
* (note that, in theory this is unlikely, as php has a broader definition for identifiers than xml-rpc method names) |
70
|
|
|
* |
71
|
|
|
* @param string $name remote function name. Will be prefixed |
72
|
1 |
|
* @param array $arguments any php value will do. For xml-rpc base64 values, wrap them in a Value object |
73
|
1 |
|
* @return mixed |
74
|
1 |
|
* |
75
|
|
|
* @throws Exception |
76
|
1 |
|
*/ |
77
|
|
|
public function call($name, $arguments) |
78
|
1 |
|
{ |
79
|
|
|
return $this->__call($name, $arguments); |
80
|
1 |
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$proxy = new XmlRpcProxy(new PhpXmlRpc\Client(XMLRPCSERVER)); |
84
|
|
|
|
85
|
|
|
$stateNo = rand(1, 51); |
86
|
|
|
// sadly, no IDE will be able to assist with autocompletion for this method, unless you manually add an equivalent phpdoc comment... |
87
|
|
|
$stateName = $proxy->getStateName($stateNo); |
|
|
|
|
88
|
|
|
|
89
|
|
|
output("State $stateNo is ".htmlspecialchars($stateName)); |
|
|
|
|
90
|
|
|
|
91
|
|
|
output("</body></html>\n"); |
92
|
|
|
|