Issues (320)

demo/client/agesort.php (1 issue)

Labels
Severity
1
<?php
2
require_once __DIR__ . "/_prepend.php";
3
4 1
output('<html lang="en">
5
<head><title>phpxmlrpc - Agesort demo</title></head>
6
<body>
7
<h1>Agesort demo</h1>
8
<h2>Send an array of "name" => "age" pairs to the server that will send it back sorted.</h2>
9
<h3>The code demonstrates usage of automatic encoding/decoding of php variables into xml-rpc values such as arrays and structs</h3>
10
<p>Have a look at <a href="../vardemo.php">vardemo.php</a> for more examples of manual encoding and decoding</p>
11
<p>You can see the source to this page here: <a href="agesort.php?showSource=1">agesort.php</a></p>
12
');
13 1
14 1
use PhpXmlRpc\Client;
15 1
use PhpXmlRpc\Encoder;
16 1
use PhpXmlRpc\Request;
17
18 1
$inAr = array(
19
    array('name' => 'Dave', 'age' => 24),
20
    array('name' => 'Edd',  'age' => 45),
21 1
    array('name' => 'Joe',  'age' => 37),
22 1
    array('name' => 'Fred', 'age' => 27),
23 1
);
24
25 1
output('This is the input data:<br/><pre>');
26 1
foreach ($inAr as $val) {
27
    output($val['name'] . ", " . $val['age'] . "\n");
28 1
}
29
output('</pre>');
30
31 1
// Create xml-rpc parameters from the input array: an array of structs
32 1
$encoder = new Encoder();
33
$v = $encoder->encode($inAr);
34
output("Encoded into xml-rpc format it looks like this: <pre>\n" . htmlentities($v->serialize()) . "</pre>\n");
35 1
36 1
// create client and request objects
37
$req = new Request('examples.sortByAge', array($v));
38
$client = new Client(XMLRPCSERVER);
39 1
40
// set maximum debug level, to have the complete communication printed to screen
41
$client->setDebug(2);
42 1
43 1
// send request
44
output('Now sending the request... (very detailed debug info follows. Scroll to the bottom of the page for results!)<hr/>');
45
$resp = $client->send($req);
46 1
output('<hr/>');
47 1
48 1
// check response for errors, and take appropriate action
49 1
if (!$resp->faultCode()) {
50 1
    output('The server gave me these results:<pre>');
51 1
    $value = $resp->value();
52 1
    foreach ($encoder->decode($value) as $struct) {
0 ignored issues
show
It seems like $value can also be of type string; however, parameter $xmlrpcVal of PhpXmlRpc\Encoder::decode() does only seem to accept PhpXmlRpc\Request|PhpXmlRpc\Value, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
    foreach ($encoder->decode(/** @scrutinizer ignore-type */ $value) as $struct) {
Loading history...
53
        // note: here we are trusting the server's response to have the expected format
54
        output(htmlspecialchars($struct['name']) . ", " . htmlspecialchars($struct['age']) . "\n");
55 1
    }
56 1
57
    output('</pre><hr/>For nerds: I got this value back<br/><pre>' .
58
        htmlentities($resp->serialize()) . "</pre><hr/>\n");
59
} else {
60
    output('An error occurred:<pre>');
61
    output('Code: ' . htmlspecialchars($resp->faultCode()) .
62
        "\nReason: '" . htmlspecialchars($resp->faultString()) . "'</pre><hr/>");
63 1
}
64
65
output("</body></html>\n");
66