WIP: Make wrap_xmlrpc_method return a closure by default instead of eval'ing a functi...
[plcapi.git] / demo / client / wrap.php
index 458f668..0b3022d 100644 (file)
@@ -1,56 +1,53 @@
 <html>
-<head><title>xmlrpc</title></head>
+<head><title>xmlrpc - Webservice wrappper demo</title></head>
 <body>
 <h1>Webservice wrappper demo</h1>
+
 <h2>Wrap methods exposed by server into php functions</h2>
-<h3>The code demonstrates usage of the most automagic client usage possible:<br/>
-1) client that returns php values instead of xmlrpcval objects<br/>
-2) wrapping of remote methods into php functions
+
+<h3>The code demonstrates usage of some the most automagic client usage possible:<br/>
+    1) client that returns php values instead of xmlrpc value objects<br/>
+    2) wrapping of remote methods into php functions<br/>
+    See also proxy.php for an alternative take
 </h3>
 <?php
-       include("xmlrpc.inc");
-       include("xmlrpc_wrappers.inc");
 
-       $c = new xmlrpc_client("/server.php", "phpxmlrpc.sourceforge.net", 80);
-       $c->return_type = 'phpvals'; // let client give us back php values instead of xmlrpcvals
-       $r =& $c->send(new xmlrpcmsg('system.listMethods'));
-       if($r->faultCode())
-       {
-               echo "<p>Server methods list could not be retrieved: error '".htmlspecialchars($r->faultString())."'</p>\n";
-       }
-       else
-       {
-               $testcase = '';
-               echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
-               foreach($r->value() as $methodname) // $r->value is an array of strings
-               {
-                       // do not wrap remote server system methods
-                       if (strpos($methodname, 'system.') !== 0)
-                       {
-                               $funcname = wrap_xmlrpc_method($c, $methodname);
-                               if($funcname)
-                               {
-                                       echo "<li>Remote server method ".htmlspecialchars($methodname)." wrapped into php function ".$funcname."</li>\n";
-                               }
-                               else
-                               {
-                                       echo "<li>Remote server method ".htmlspecialchars($methodname)." could not be wrapped!</li>\n";
-                               }
-                               if($methodname == 'examples.getStateName')
-                               {
-                                       $testcase = $funcname;
-                               }
-                       }
-               }
-               echo "</ul>\n";
-               if($testcase)
-               {
-                       echo "Now testing function $testcase: remote method to convert U.S. state number into state name";
-                       $statenum = 25;
-                       $statename = $testcase($statenum, 2);
-                       echo "State number $statenum is ".htmlspecialchars($statename);
-               }
-       }
+include_once __DIR__ . "/../../src/Autoloader.php";
+PhpXmlRpc\Autoloader::register();
+
+$client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
+$client->return_type = 'phpvals'; // let client give us back php values instead of xmlrpcvals
+$resp = $client->send(new PhpXmlRpc\Request('system.listMethods'));
+if ($resp->faultCode()) {
+    echo "<p>Server methods list could not be retrieved: error {$resp->faultCode()} '" . htmlspecialchars($resp->faultString()) . "'</p>\n";
+} else {
+    echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
+    flush();
+
+    $callable = false;
+    $wrapper = new PhpXmlRpc\Wrapper();
+    foreach ($resp->value() as $methodName) {
+        // $resp->value is an array of strings
+        if ($methodName == 'examples.getStateName') {
+            $callable = $wrapper->wrap_xmlrpc_method($client, $methodName);
+            if ($callable) {
+                echo "<li>Remote server method " . htmlspecialchars($methodName) . " wrapped into php function</li>\n";
+            } else {
+                echo "<li>Remote server method " . htmlspecialchars($methodName) . " could not be wrapped!</li>\n";
+            }
+            break;
+        }
+    }
+    echo "</ul>\n";
+    flush();
+
+    if ($callable) {
+        echo "Now testing function for remote method to convert U.S. state number into state name";
+        $stateNum = rand(1, 51);
+        // the 2nd parameter gets added to the closure - it is teh debug level to be used for the client
+        $stateName = $callable($stateNum, 2);
+    }
+}
 ?>
 </body>
 </html>