Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / client / introspect.php
index 55dec8a..5c35950 100644 (file)
@@ -1 +1,88 @@
-<html>\r\r<head><title>xmlrpc</title></head>\r\r<body>\r\r<h1>Introspect demo</h1>\r\r<h2>Query server for available methods and their description</h2>\r\r<h3>The code demonstrates usage of multicall and introspection methods</h3>\r\r<?php\r\r  include("xmlrpc.inc");\r\r\r\r      function display_error($r)\r\r    {\r\r             print "An error occurred: ";\r\r          print "Code: " . $r->faultCode()\r\r                      . " Reason: '" .$r->faultString()."'<br/>";\r\r   }\r\r\r\r   // 'new style' client constuctor\r\r      $c = new xmlrpc_client("http://phpxmlrpc.sourceforge.net/server.php");\r\r        print "<h3>methods available at http://" . $c->server . $c->path .  "</h3>\n";\r\r\r\r      $m = new xmlrpcmsg('system.listMethods');\r\r     $r =& $c->send($m);\r\r   if($r->faultCode())\r\r   {\r\r             display_error($r);\r\r    }\r\r     else\r\r  {\r\r             $v=$r->value();\r\r               for($i=0; $i<$v->arraysize(); $i++)\r\r           {\r\r                     $mname=$v->arraymem($i);\r\r                      print "<h4>" . $mname->scalarval() . "</h4>\n";\r\r\r\r                     // build messages first, add params later\r\r                     $m1  = new xmlrpcmsg('system.methodHelp');\r\r                    $m2  = new xmlrpcmsg('system.methodSignature');\r\r                       $val = new xmlrpcval($mname->scalarval(), "string");\r\r                  $m1->addParam($val);\r\r                  $m2->addParam($val);\r\r\r\r                        // send multiple messages in one pass.\r\r                        // If server does not support multicall, client will fall back to 2 separate calls\r\r                    $ms = array($m1, $m2);\r\r                        $rs =& $c->send($ms);\r\r\r\r                       if($rs[0]->faultCode())\r\r                       {\r\r                             display_error($rs[0]);\r\r                        }\r\r                     else\r\r                  {\r\r                             $val=$rs[0]->value();\r\r                         $txt=$val->scalarval();\r\r                               if($txt != "")\r\r                                {\r\r                                     print "<h4>Documentation</h4><p>${txt}</p>\n";\r\r                                }\r\r                             else\r\r                          {\r\r                                     print "<p>No documentation available.</p>\n";\r\r                         }\r\r                     }\r\r\r\r                   if($rs[1]->faultCode())\r\r                       {\r\r                             display_error($rs[1]);\r\r                        }\r\r                     else\r\r                  {\r\r                             print "<h4>Signature</h4><p>\n";\r\r                              $val = $rs[1]->value();\r\r                               if($val->kindOf()=="array")\r\r                           {\r\r                                     for($j=0; $j<$val->arraysize(); $j++)\r\r                                 {\r\r                                             $x = $val->arraymem($j);\r\r                                              $ret = $x->arraymem(0);\r\r                                               print "<code>" . $ret->scalarval() . " "\r\r                                                      . $mname->scalarval() . "(";\r\r                                          if($x->arraysize()>1)\r\r                                         {\r\r                                                     for($k=1; $k<$x->arraysize(); $k++)\r\r                                                   {\r\r                                                             $y = $x->arraymem($k);\r\r                                                                print $y->scalarval();\r\r                                                                if($k < $x->arraysize()-1)\r\r                                                            {\r\r                                                                     print ", ";\r\r                                                           }\r\r                                                     }\r\r                                             }\r\r                                             print ")</code><br/>\n";\r\r                                      }\r\r                             }\r\r                             else\r\r                          {\r\r                                     print "Signature unknown\n";\r\r                          }\r\r                             print "</p>\n";\r\r                       }\r\r             }\r\r     }\r\r?>\r\r</body>\r\r</html>\r\r
\ No newline at end of file
+<html>
+<head><title>xmlrpc - Introspect demo</title></head>
+<body>
+<h1>Introspect demo</h1>
+<h2>Query server for available methods and their description</h2>
+<h3>The code demonstrates usage of multicall and introspection methods</h3>
+<?php
+
+include_once __DIR__ . "/../../src/Autoloader.php";
+PhpXmlRpc\Autoloader::register();
+
+function display_error($r)
+{
+    print "An error occurred: ";
+    print "Code: " . $r->faultCode()
+        . " Reason: '" . $r->faultString() . "'<br/>";
+}
+
+$client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
+
+// First off, let's retrieve the list of methods available on the remote server
+print "<h3>methods available at http://" . $client->server . $client->path . "</h3>\n";
+$req = new PhpXmlRpc\Request('system.listMethods');
+$resp = $client->send($req);
+
+if ($resp->faultCode()) {
+    display_error($resp);
+} else {
+    $v = $resp->value();
+
+    // Then, retrieve the signature and help text of each available method
+    foreach ($v as $methodName) {
+        print "<h4>" . $methodName->scalarval() . "</h4>\n";
+        // build messages first, add params later
+        $m1 = new PhpXmlRpc\Request('system.methodHelp');
+        $m2 = new PhpXmlRpc\Request('system.methodSignature');
+        $val = new PhpXmlRpc\Value($methodName->scalarval(), "string");
+        $m1->addParam($val);
+        $m2->addParam($val);
+        // Send multiple requests in one http call.
+        // If server does not support multicall, client will automatically fall back to 2 separate calls
+        $ms = array($m1, $m2);
+        $rs = $client->send($ms);
+        if ($rs[0]->faultCode()) {
+            display_error($rs[0]);
+        } else {
+            $val = $rs[0]->value();
+            $txt = $val->scalarval();
+            if ($txt != "") {
+                print "<h4>Documentation</h4><p>${txt}</p>\n";
+            } else {
+                print "<p>No documentation available.</p>\n";
+            }
+        }
+        if ($rs[1]->faultCode()) {
+            display_error($rs[1]);
+        } else {
+            print "<h4>Signature</h4><p>\n";
+            // note: using PhpXmlRpc\Encoder::decode() here would lead to cleaner code
+            $val = $rs[1]->value();
+            if ($val->kindOf() == "array") {
+                foreach ($val as $x) {
+                    //$ret = $x->arraymem(0);
+                    $ret = $x[0];
+                    print "<code>" . $ret->scalarval() . " "
+                        . $methodName->scalarval() . "(";
+                    if ($x->count() > 1) {
+                        for ($k = 1; $k < $x->count(); $k++) {
+                            //$y = $x->arraymem($k);
+                            $y = $x[$k];
+                            print $y->scalarval();
+                            if ($k < $x->count() - 1) {
+                                print ", ";
+                            }
+                        }
+                    }
+                    print ")</code><br/>\n";
+                }
+            } else {
+                print "Signature unknown\n";
+            }
+            print "</p>\n";
+        }
+    }
+}
+?>
+</body>
+</html>