Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / client / agesort.php
1 <html>
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></p>
11 <?php
12
13 include_once __DIR__ . "/../../src/Autoloader.php";
14 PhpXmlRpc\Autoloader::register();
15
16 $inAr = array("Dave" => 24, "Edd" => 45, "Joe" => 37, "Fred" => 27);
17 print "This is the input data:<br/><pre>";
18 foreach($inAr as $key => $val) {
19     print $key . ", " . $val . "\n";
20 }
21 print "</pre>";
22
23 // create parameters from the input array: an xmlrpc array of xmlrpc structs
24 $p = array();
25 foreach ($inAr as $key => $val) {
26     $p[] = new PhpXmlRpc\Value(
27         array(
28             "name" => new PhpXmlRpc\Value($key),
29             "age" => new PhpXmlRpc\Value($val, "int")
30         ),
31         "struct"
32     );
33 }
34 $v = new PhpXmlRpc\Value($p, "array");
35 print "Encoded into xmlrpc format it looks like this: <pre>\n" . htmlentities($v->serialize()) . "</pre>\n";
36
37 // create client and message objects
38 $req = new PhpXmlRpc\Request('examples.sortByAge', array($v));
39 $client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
40
41 // set maximum debug level, to have the complete communication printed to screen
42 $client->setDebug(2);
43
44 // send request
45 print "Now sending request (detailed debug info follows)";
46 $resp = $client->send($req);
47
48 // check response for errors, and take appropriate action
49 if (!$resp->faultCode()) {
50     print "The server gave me these results:<pre>";
51     $value = $resp->value();
52     foreach ($value as $struct) {
53         //$name = $struct->structmem("name");
54         $name = $struct["name"];
55         //$age = $struct->structmem("age");
56         $age = $struct["age"];
57         print htmlspecialchars($name->scalarval()) . ", " . htmlspecialchars($age->scalarval()) . "\n";
58     }
59
60     print "<hr/>For nerds: I got this value back<br/><pre>" .
61         htmlentities($resp->serialize()) . "</pre><hr/>\n";
62 } else {
63     print "An error occurred:<pre>";
64     print "Code: " . htmlspecialchars($resp->faultCode()) .
65         "\nReason: '" . htmlspecialchars($resp->faultString()) . '\'</pre><hr/>';
66 }
67
68 ?>
69 </body>
70 </html>