merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / demo / client / agesort.php
1 <?php require_once __DIR__ . "/_prepend.php"; ?><html lang="en">
2 <head><title>xmlrpc - Agesort demo</title></head>
3 <body>
4 <h1>Agesort demo</h1>
5
6 <h2>Send an array of 'name' => 'age' pairs to the server that will send it back sorted.</h2>
7
8 <h3>The source code demonstrates basic lib usage, including handling of xmlrpc arrays and structs</h3>
9
10 <p>You can see the source to this page here: <a href="agesort.php?showSource=1">agesort.php</a></p>
11 <?php
12
13 $inAr = array("Dave" => 24, "Edd" => 45, "Joe" => 37, "Fred" => 27);
14 print "This is the input data:<br/><pre>";
15 foreach ($inAr as $key => $val) {
16     print $key . ", " . $val . "\n";
17 }
18 print "</pre>";
19
20 // Create parameters from the input array: an xmlrpc array of xmlrpc structs
21 $p = array();
22 foreach ($inAr as $key => $val) {
23     $p[] = new PhpXmlRpc\Value(
24         array(
25             "name" => new PhpXmlRpc\Value($key),
26             "age" => new PhpXmlRpc\Value($val, "int")
27         ),
28         "struct"
29     );
30 }
31 $v = new PhpXmlRpc\Value($p, "array");
32 print "Encoded into xmlrpc format it looks like this: <pre>\n" . htmlentities($v->serialize()) . "</pre>\n";
33
34 // create client and message objects
35 $req = new PhpXmlRpc\Request('examples.sortByAge', array($v));
36 $client = new PhpXmlRpc\Client(XMLRPCSERVER);
37
38 // set maximum debug level, to have the complete communication printed to screen
39 $client->setDebug(2);
40
41 // send request
42 print "Now sending request (detailed debug info follows)";
43 $resp = $client->send($req);
44
45 // check response for errors, and take appropriate action
46 if (!$resp->faultCode()) {
47     print "The server gave me these results:<pre>";
48     $value = $resp->value();
49     foreach ($value as $struct) {
50         $name = $struct["name"];
51         $age = $struct["age"];
52         print htmlspecialchars($name->scalarval()) . ", " . htmlspecialchars($age->scalarval()) . "\n";
53     }
54
55     print "<hr/>For nerds: I got this value back<br/><pre>" .
56         htmlentities($resp->serialize()) . "</pre><hr/>\n";
57 } else {
58     print "An error occurred:<pre>";
59     print "Code: " . htmlspecialchars($resp->faultCode()) .
60         "\nReason: '" . htmlspecialchars($resp->faultString()) . '\'</pre><hr/>';
61 }
62
63 ?>
64 </body>
65 </html><?php require_once __DIR__ . "/_append.php"; ?>