Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / client / introspect.php
1 <html>
2 <head><title>xmlrpc - Introspect demo</title></head>
3 <body>
4 <h1>Introspect demo</h1>
5 <h2>Query server for available methods and their description</h2>
6 <h3>The code demonstrates usage of multicall and introspection methods</h3>
7 <?php
8
9 include_once __DIR__ . "/../../src/Autoloader.php";
10 PhpXmlRpc\Autoloader::register();
11
12 function display_error($r)
13 {
14     print "An error occurred: ";
15     print "Code: " . $r->faultCode()
16         . " Reason: '" . $r->faultString() . "'<br/>";
17 }
18
19 $client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
20
21 // First off, let's retrieve the list of methods available on the remote server
22 print "<h3>methods available at http://" . $client->server . $client->path . "</h3>\n";
23 $req = new PhpXmlRpc\Request('system.listMethods');
24 $resp = $client->send($req);
25
26 if ($resp->faultCode()) {
27     display_error($resp);
28 } else {
29     $v = $resp->value();
30
31     // Then, retrieve the signature and help text of each available method
32     foreach ($v as $methodName) {
33         print "<h4>" . $methodName->scalarval() . "</h4>\n";
34         // build messages first, add params later
35         $m1 = new PhpXmlRpc\Request('system.methodHelp');
36         $m2 = new PhpXmlRpc\Request('system.methodSignature');
37         $val = new PhpXmlRpc\Value($methodName->scalarval(), "string");
38         $m1->addParam($val);
39         $m2->addParam($val);
40         // Send multiple requests in one http call.
41         // If server does not support multicall, client will automatically fall back to 2 separate calls
42         $ms = array($m1, $m2);
43         $rs = $client->send($ms);
44         if ($rs[0]->faultCode()) {
45             display_error($rs[0]);
46         } else {
47             $val = $rs[0]->value();
48             $txt = $val->scalarval();
49             if ($txt != "") {
50                 print "<h4>Documentation</h4><p>${txt}</p>\n";
51             } else {
52                 print "<p>No documentation available.</p>\n";
53             }
54         }
55         if ($rs[1]->faultCode()) {
56             display_error($rs[1]);
57         } else {
58             print "<h4>Signature</h4><p>\n";
59             // note: using PhpXmlRpc\Encoder::decode() here would lead to cleaner code
60             $val = $rs[1]->value();
61             if ($val->kindOf() == "array") {
62                 foreach ($val as $x) {
63                     //$ret = $x->arraymem(0);
64                     $ret = $x[0];
65                     print "<code>" . $ret->scalarval() . " "
66                         . $methodName->scalarval() . "(";
67                     if ($x->count() > 1) {
68                         for ($k = 1; $k < $x->count(); $k++) {
69                             //$y = $x->arraymem($k);
70                             $y = $x[$k];
71                             print $y->scalarval();
72                             if ($k < $x->count() - 1) {
73                                 print ", ";
74                             }
75                         }
76                     }
77                     print ")</code><br/>\n";
78                 }
79             } else {
80                 print "Signature unknown\n";
81             }
82             print "</p>\n";
83         }
84     }
85 }
86 ?>
87 </body>
88 </html>