merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / demo / client / wrap.php
1 <?php require_once __DIR__ . "/_prepend.php"; ?><html lang="en">
2 <head><title>xmlrpc - Webservice wrappper demo</title></head>
3 <body>
4 <h1>Webservice wrappper demo</h1>
5
6 <h2>Wrap methods exposed by server into php functions</h2>
7
8 <h3>The code demonstrates usage of some the most automagic client usage possible:<br/>
9     1) client that returns php values instead of xmlrpc value objects<br/>
10     2) wrapping of remote methods into php functions<br/>
11     See also proxy.php for an alternative take
12 </h3>
13 <p>You can see the source to this page here: <a href="wrap.php?showSource=1">wrap.php</a></p>
14 <?php
15
16 $client = new PhpXmlRpc\Client(XMLRPCSERVER);
17 $client->return_type = 'phpvals'; // let client give us back php values instead of xmlrpcvals
18 $resp = $client->send(new PhpXmlRpc\Request('system.listMethods'));
19 if ($resp->faultCode()) {
20     echo "<p>Server methods list could not be retrieved: error {$resp->faultCode()} '" . htmlspecialchars($resp->faultString()) . "'</p>\n";
21 } else {
22     echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
23     flush();
24
25     $callable = false;
26     $wrapper = new PhpXmlRpc\Wrapper();
27     foreach ($resp->value() as $methodName) {
28         // $resp->value is an array of strings
29         if ($methodName == 'examples.getStateName') {
30             $callable = $wrapper->wrapXmlrpcMethod($client, $methodName);
31             if ($callable) {
32                 echo "<li>Remote server method " . htmlspecialchars($methodName) . " wrapped into php function</li>\n";
33             } else {
34                 echo "<li>Remote server method " . htmlspecialchars($methodName) . " could not be wrapped!</li>\n";
35             }
36             break;
37         }
38     }
39     echo "</ul>\n";
40     flush();
41
42     if ($callable) {
43         echo "Now testing function for remote method to convert U.S. state number into state name";
44         $stateNum = rand(1, 51);
45         // the 2nd parameter gets added to the closure - it is the debug level to be used for the client
46         $stateName = $callable($stateNum, 2);
47         echo "State $stateNum is ".htmlspecialchars($stateName);
48     }
49 }
50 ?>
51 </body>
52 </html><?php require_once __DIR__ . "/_append.php"; ?>