Refactor demo files (client side): remove redundant ones; make sure all use existing...
[plcapi.git] / demo / client / wrap.php
1 <html>
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 <?php
14
15 include_once __DIR__ . "/../../src/Autoloader.php";
16 PhpXmlRpc\Autoloader::register();
17
18 $client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
19 $client->return_type = 'phpvals'; // let client give us back php values instead of xmlrpcvals
20 $resp = $client->send(new PhpXmlRpc\Request('system.listMethods'));
21 if ($resp->faultCode()) {
22     echo "<p>Server methods list could not be retrieved: error {$resp->faultCode()} '" . htmlspecialchars($resp->faultString()) . "'</p>\n";
23 } else {
24     $testCase = '';
25     $wrapper = new PhpXmlRpc\Wrapper();
26     echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
27     foreach ($resp->value() as $methodName) {
28         // $resp->value is an array of strings
29
30         // do not wrap remote server system methods
31         if (strpos($methodName, 'system.') !== 0) {
32             $funcName = $wrapper->wrap_xmlrpc_method($client, $methodName);
33             if ($funcName) {
34                 echo "<li>Remote server method " . htmlspecialchars($methodName) . " wrapped into php function " . $funcName . "</li>\n";
35             } else {
36                 echo "<li>Remote server method " . htmlspecialchars($methodName) . " could not be wrapped!</li>\n";
37             }
38             if ($methodName == 'examples.getStateName') {
39                 $testCase = $funcName;
40             }
41         }
42     }
43     echo "</ul>\n";
44     if ($testCase) {
45         echo "Now testing function $testCase: remote method to convert U.S. state number into state name";
46         $stateNum = rand(1, 51);
47         $stateName = $testCase($stateNum, 2);
48         echo "State number $stateNum is " . htmlspecialchars($stateName);
49     }
50 }
51 ?>
52 </body>
53 </html>