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