Start clean up of demo code: use new API, camelCase variables, readable variables...
[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 // 'new style' client constructor
19 $client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
20 print "<h3>methods available at http://" . $client->server . $client->path . "</h3>\n";
21 $req = new PhpXmlRpc\Request('system.listMethods');
22 $resp = $client->send($req);
23 if ($resp->faultCode()) {
24     display_error($resp);
25 } else {
26     $v = $resp->value();
27     for ($i = 0; $i < $v->arraysize(); $i++) {
28         $mname = $v->arraymem($i);
29         print "<h4>" . $mname->scalarval() . "</h4>\n";
30         // build messages first, add params later
31         $m1 = new PhpXmlRpc\Request('system.methodHelp');
32         $m2 = new PhpXmlRpc\Request('system.methodSignature');
33         $val = new PhpXmlRpc\Value($mname->scalarval(), "string");
34         $m1->addParam($val);
35         $m2->addParam($val);
36         // send multiple messages in one pass.
37         // If server does not support multicall, client will fall back to 2 separate calls
38         $ms = array($m1, $m2);
39         $rs = $client->send($ms);
40         if ($rs[0]->faultCode()) {
41             display_error($rs[0]);
42         } else {
43             $val = $rs[0]->value();
44             $txt = $val->scalarval();
45             if ($txt != "") {
46                 print "<h4>Documentation</h4><p>${txt}</p>\n";
47             } else {
48                 print "<p>No documentation available.</p>\n";
49             }
50         }
51         if ($rs[1]->faultCode()) {
52             display_error($rs[1]);
53         } else {
54             print "<h4>Signature</h4><p>\n";
55             $val = $rs[1]->value();
56             if ($val->kindOf() == "array") {
57                 for ($j = 0; $j < $val->arraysize(); $j++) {
58                     $x = $val->arraymem($j);
59                     $ret = $x->arraymem(0);
60                     print "<code>" . $ret->scalarval() . " "
61                         . $mname->scalarval() . "(";
62                     if ($x->arraysize() > 1) {
63                         for ($k = 1; $k < $x->arraysize(); $k++) {
64                             $y = $x->arraymem($k);
65                             print $y->scalarval();
66                             if ($k < $x->arraysize() - 1) {
67                                 print ", ";
68                             }
69                         }
70                     }
71                     print ")</code><br/>\n";
72                 }
73             } else {
74                 print "Signature unknown\n";
75             }
76             print "</p>\n";
77         }
78     }
79 }
80 ?>
81 </body>
82 </html>