Merge pull request #96 from duzun/patch-1
[plcapi.git] / demo / client / wrap.php
index 13ffabb..4e3db40 100644 (file)
@@ -1,52 +1,52 @@
-<html>
+<?php require_once __DIR__ . "/_prepend.php"; ?><html lang="en">
 <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/>
+<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
+    2) wrapping of remote methods into php functions<br/>
+    See also proxy.php for an alternative take
 </h3>
+<p>You can see the source to this page here: <a href="wrap.php?showSource=1">wrap.php</a></p>
 <?php
 
-include_once __DIR__ . "/../../src/Autoloader.php";
-PhpXmlRpc\Autoloader::register();
-
-$client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
+$client = new PhpXmlRpc\Client(XMLRPCSERVER);
 $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 '" . htmlspecialchars($r->faultString()) . "'</p>\n";
+    echo "<p>Server methods list could not be retrieved: error {$resp->faultCode()} '" . htmlspecialchars($resp->faultString()) . "'</p>\n";
 } else {
-    $testCase = '';
-    $wrapper = new PhpXmlRpc\Wrapper();
     echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
-    foreach ($resp->value() as $methodName) {
-        // $r->value is an array of strings
+    flush();
 
-        // do not wrap remote server system methods
-        if (strpos($methodName, 'system.') !== 0) {
-            $funcName = $wrapper->wrap_xmlrpc_method($client, $methodName);
-            if ($funcName) {
-                echo "<li>Remote server method " . htmlspecialchars($methodName) . " wrapped into php function " . $funcName . "</li>\n";
+    $callable = false;
+    $wrapper = new PhpXmlRpc\Wrapper();
+    foreach ($resp->value() as $methodName) {
+        // $resp->value is an array of strings
+        if ($methodName == 'examples.getStateName') {
+            $callable = $wrapper->wrapXmlrpcMethod($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";
             }
-            if ($methodName == 'examples.getStateName') {
-                $testCase = $funcName;
-            }
+            break;
         }
     }
     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);
+    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 the debug level to be used for the client
+        $stateName = $callable($stateNum, 2);
+        echo "State $stateNum is ".htmlspecialchars($stateName);
     }
 }
 ?>
 </body>
-</html>
+</html><?php require_once __DIR__ . "/_append.php"; ?>