merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / demo / client / introspect.php
1 <?php require_once __DIR__ . "/_prepend.php"; ?><html lang="en">
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 <p>You can see the source to this page here: <a href="introspect.php?showSource=1">introspect.php</a></p>
8 <?php
9
10 function display_error($r)
11 {
12     print "An error occurred: ";
13     print "Code: " . $r->faultCode()
14         . " Reason: '" . $r->faultString() . "'<br/>";
15 }
16
17 $client = new PhpXmlRpc\Client(XMLRPCSERVER);
18
19 // First off, let's retrieve the list of methods available on the remote server
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
24 if ($resp->faultCode()) {
25     display_error($resp);
26 } else {
27     $v = $resp->value();
28
29     // Then, retrieve the signature and help text of each available method
30     foreach ($v as $methodName) {
31         print "<h4>" . $methodName->scalarval() . "</h4>\n";
32         // build messages first, add params later
33         $m1 = new PhpXmlRpc\Request('system.methodHelp');
34         $m2 = new PhpXmlRpc\Request('system.methodSignature');
35         $val = new PhpXmlRpc\Value($methodName->scalarval(), "string");
36         $m1->addParam($val);
37         $m2->addParam($val);
38         // Send multiple requests in one http call.
39         // If server does not support multicall, client will automatically fall back to 2 separate calls
40         $ms = array($m1, $m2);
41         $rs = $client->send($ms);
42         if ($rs[0]->faultCode()) {
43             display_error($rs[0]);
44         } else {
45             $val = $rs[0]->value();
46             $txt = $val->scalarval();
47             if ($txt != "") {
48                 print "<h4>Documentation</h4><p>${txt}</p>\n";
49             } else {
50                 print "<p>No documentation available.</p>\n";
51             }
52         }
53         if ($rs[1]->faultCode()) {
54             display_error($rs[1]);
55         } else {
56             print "<h4>Signature</h4><p>\n";
57             // note: using PhpXmlRpc\Encoder::decode() here would lead to cleaner code
58             $val = $rs[1]->value();
59             if ($val->kindOf() == "array") {
60                 foreach ($val as $x) {
61                     $ret = $x[0];
62                     print "<code>" . htmlspecialchars($ret->scalarval()) . " "
63                         . htmlspecialchars($methodName->scalarval()) . "(";
64                     if ($x->count() > 1) {
65                         for ($k = 1; $k < $x->count(); $k++) {
66                             $y = $x[$k];
67                             print htmlspecialchars($y->scalarval());
68                             if ($k < $x->count() - 1) {
69                                 print ", ";
70                             }
71                         }
72                     }
73                     print ")</code><br/>\n";
74                 }
75             } else {
76                 print "Signature unknown\n";
77             }
78             print "</p>\n";
79         }
80     }
81 }
82 ?>
83 </body>
84 </html><?php require_once __DIR__ . "/_append.php"; ?>