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[0]; |
64
|
|
|
print "<code>" . $ret->scalarval() . " " |
65
|
|
|
. $methodName->scalarval() . "("; |
66
|
|
|
if ($x->count() > 1) { |
67
|
|
|
for ($k = 1; $k < $x->count(); $k++) { |
68
|
|
|
$y = $x[$k]; |
69
|
|
|
print $y->scalarval(); |
70
|
|
|
if ($k < $x->count() - 1) { |
71
|
|
|
print ", "; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
print ")</code><br/>\n"; |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
print "Signature unknown\n"; |
79
|
|
|
} |
80
|
|
|
print "</p>\n"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
?> |
85
|
|
|
</body> |
86
|
|
|
</html> |
87
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.