Implement interface ArrayAccess in the Value class
[plcapi.git] / demo / client / agesort.php
index 7fa16af..0b6aa9a 100644 (file)
@@ -1 +1,70 @@
-<html>\r<head><title>xmlrpc</title></head>\r<body>\r<h1>Agesort demo</h1>\r<h2>Send an array of 'name' => 'age' pairs to the server that will send it back sorted.</h2>\r<h3>The source code demonstrates basic lib usage, including handling of xmlrpc arrays and structs</h3>\r<p></p>\r<?php\rinclude("xmlrpc.inc");\r\r$inAr=array("Dave" => 24, "Edd" => 45, "Joe" => 37, "Fred" => 27);\rreset($inAr);\rprint "This is the input data:<br/><pre>";\rwhile (list($key, $val)=each($inAr)) {\r  print $key . ", " . $val . "\n";\r}\rprint "</pre>";\r\r// create parameters from the input array: an xmlrpc array of xmlrpc structs\r$p=array();\rforeach($inAr as $key => $val) {\r  $p[]=new xmlrpcval(array("name" => new xmlrpcval($key),\r                           "age" => new xmlrpcval($val, "int")), "struct");\r}\r$v=new xmlrpcval($p, "array");\rprint "Encoded into xmlrpc format it looks like this: <pre>\n" .  htmlentities($v->serialize()). "</pre>\n";\r\r// create client and message objects\r$f=new xmlrpcmsg('examples.sortByAge',  array($v));\r$c=new xmlrpc_client("/server.php", "phpxmlrpc.sourceforge.net", 80);\r\r// set maximum debug level, to have the complete communication printed to screen\r$c->setDebug(2);\r\r// send request\rprint "Now sending request (detailed debug info follows)";\r$r=&$c->send($f);\r\r// check response for errors, and take appropriate action\rif (!$r->faultCode()) {\r  print "The server gave me these results:<pre>";\r  $v=$r->value();\r  $max=$v->arraysize();\r  for($i=0; $i<$max; $i++) {\r    $rec=$v->arraymem($i);\r    $n=$rec->structmem("name");\r    $a=$rec->structmem("age");\r    print htmlspecialchars($n->scalarval()) . ", " . htmlspecialchars($a->scalarval()) . "\n";\r  }\r\r  print "<hr/>For nerds: I got this value back<br/><pre>" .\r    htmlentities($r->serialize()). "</pre><hr/>\n";\r} else {\r  print "An error occurred:<pre>";\r  print "Code: " . htmlspecialchars($r->faultCode()) .\r    "\nReason: '" . htmlspecialchars($r->faultString()).'\'</pre><hr/>';\r}\r\r?>\r<em>$Id$</em>\r</body>\r</html>\r
\ No newline at end of file
+<html>
+<head><title>xmlrpc - Agesort demo</title></head>
+<body>
+<h1>Agesort demo</h1>
+
+<h2>Send an array of 'name' => 'age' pairs to the server that will send it back sorted.</h2>
+
+<h3>The source code demonstrates basic lib usage, including handling of xmlrpc arrays and structs</h3>
+
+<p></p>
+<?php
+
+include_once __DIR__ . "/../../src/Autoloader.php";
+PhpXmlRpc\Autoloader::register();
+
+$inAr = array("Dave" => 24, "Edd" => 45, "Joe" => 37, "Fred" => 27);
+print "This is the input data:<br/><pre>";
+foreach($inAr as $key => $val) {
+    print $key . ", " . $val . "\n";
+}
+print "</pre>";
+
+// create parameters from the input array: an xmlrpc array of xmlrpc structs
+$p = array();
+foreach ($inAr as $key => $val) {
+    $p[] = new PhpXmlRpc\Value(
+        array(
+            "name" => new PhpXmlRpc\Value($key),
+            "age" => new PhpXmlRpc\Value($val, "int")
+        ),
+        "struct"
+    );
+}
+$v = new PhpXmlRpc\Value($p, "array");
+print "Encoded into xmlrpc format it looks like this: <pre>\n" . htmlentities($v->serialize()) . "</pre>\n";
+
+// create client and message objects
+$req = new PhpXmlRpc\Request('examples.sortByAge', array($v));
+$client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
+
+// set maximum debug level, to have the complete communication printed to screen
+$client->setDebug(2);
+
+// send request
+print "Now sending request (detailed debug info follows)";
+$resp = $client->send($req);
+
+// check response for errors, and take appropriate action
+if (!$resp->faultCode()) {
+    print "The server gave me these results:<pre>";
+    $value = $resp->value();
+    foreach ($value as $struct) {
+        //$name = $struct->structmem("name");
+        $name = $struct["name"];
+        //$age = $struct->structmem("age");
+        $age = $struct["age"];
+        print htmlspecialchars($name->scalarval()) . ", " . htmlspecialchars($age->scalarval()) . "\n";
+    }
+
+    print "<hr/>For nerds: I got this value back<br/><pre>" .
+        htmlentities($resp->serialize()) . "</pre><hr/>\n";
+} else {
+    print "An error occurred:<pre>";
+    print "Code: " . htmlspecialchars($resp->faultCode()) .
+        "\nReason: '" . htmlspecialchars($resp->faultString()) . '\'</pre><hr/>';
+}
+
+?>
+</body>
+</html>