Refactor demo files (client side): remove redundant ones; make sure all use existing...
authorgggeek <giunta.gaetano@gmail.com>
Tue, 12 May 2015 22:49:19 +0000 (23:49 +0100)
committergggeek <giunta.gaetano@gmail.com>
Tue, 12 May 2015 22:49:19 +0000 (23:49 +0100)
demo/client/comment.php [deleted file]
demo/client/getstatename.php [moved from demo/client/client.php with 81% similarity]
demo/client/introspect.php
demo/client/mail.php
demo/client/proxy.php [new file with mode: 0644]
demo/client/simple_call.php [deleted file]
demo/client/wrap.php
demo/client/zopetest.php [deleted file]
tests/5DemofilesTest.php

diff --git a/demo/client/comment.php b/demo/client/comment.php
deleted file mode 100644 (file)
index 2c34d3f..0000000
+++ /dev/null
@@ -1,211 +0,0 @@
-<?php
-
-include_once __DIR__ . "/../../src/Autoloader.php";
-PhpXmlRpc\Autoloader::register();
-
-$mydir = "demo/";
-
-// define some utility functions
-function bomb()
-{
-    print "</body></html>";
-    exit();
-}
-
-function dispatch($client, $method, $args)
-{
-    $req = new PhpXmlRpc\Request($method, $args);
-    $resp = $client->send($req);
-    if (!$resp) {
-        print "<p>IO error: " . $client->errstr . "</p>";
-        bomb();
-    }
-    if ($resp->faultCode()) {
-        print "<p>There was an error: " . $resp->faultCode() . " " .
-            $resp->faultString() . "</p>";
-        bomb();
-    }
-
-    $encoder = new PhpXmlRpc\Encoder();
-    return $encoder->decode($resp->value());
-}
-
-// create client for discussion server
-$dclient = new PhpXmlRpc\Client("http://xmlrpc.usefulinc.com/${mydir}discuss.php");
-
-// check if we're posting a comment, and send it if so
-@$storyid = $_POST["storyid"];
-if ($storyid) {
-
-    //    print "Returning to " . $HTTP_POST_VARS["returnto"];
-
-    $res = dispatch($dclient, "discuss.addComment",
-        array(new PhpXmlRpc\Value($storyid),
-            new PhpXmlRpc\Value(stripslashes(@$_POST["name"])),
-            new PhpXmlRpc\Value(stripslashes(@$_POST["commenttext"])),));
-
-    // send the browser back to the originating page
-    Header("Location: ${mydir}/comment.php?catid=" .
-        $_POST["catid"] . "&chanid=" .
-        $_POST["chanid"] . "&oc=" .
-        $_POST["catid"]);
-    exit(0);
-}
-
-// now we've got here, we're exploring the story store
-
-?>
-<html>
-<head><title>meerkat browser</title></head>
-<body bgcolor="#ffffff">
-<h2>Meerkat integration</h2>
-<?php
-@$catid = $_GET["catid"];
-if (@$_GET["oc"] == $catid) {
-    @$chanid = $_GET["chanid"];
-} else {
-    $chanid = 0;
-}
-
-$client = new PhpXmlRpc\Client("http://www.oreillynet.com/meerkat/xml-rpc/server.php");
-
-if (@$_GET["comment"] &&
-    (!@$_GET["cdone"])
-) {
-    // we're making a comment on a story,
-    // so display a comment form
-    ?>
-    <h3>Make a comment on the story</h3>
-    <form method="post">
-        <p>Your name:<br/><input type="text" size="30" name="name"/></p>
-
-        <p>Your comment:<br/><textarea rows="5" cols="60"
-                                       name="commenttext"></textarea></p>
-        <input type="submit" value="Send comment"/>
-        <input type="hidden" name="storyid"
-               value="<?php echo @$_GET["comment"];
-               ?>"/>
-        <input type="hidden" name="chanid"
-               value="<?php echo $chanid;
-               ?>"/>
-        <input type="hidden" name="catid"
-               value="<?php echo $catid;
-               ?>"/>
-
-    </form>
-<?php
-
-} else {
-    $categories = dispatch($client, "meerkat.getCategories", array());
-    if ($catid) {
-        $sources = dispatch($client, "meerkat.getChannelsByCategory",
-            array(new PhpXmlRpc\Value($catid, "int")));
-    }
-    if ($chanid) {
-        $stories = dispatch($client, "meerkat.getItems",
-            array(new PhpXmlRpc\Value(
-                array(
-                    "channel" => new PhpXmlRpc\Value($chanid, "int"),
-                    "ids" => new PhpXmlRpc\Value(1, "int"),
-                    "descriptions" => new PhpXmlRpc\Value(200, "int"),
-                    "num_items" => new PhpXmlRpc\Value(5, "int"),
-                    "dates" => new PhpXmlRpc\Value(0, "int"),
-                ), "struct")));
-    }
-    ?>
-    <form>
-        <p>Subject area:<br/>
-            <select name="catid">
-                <?php
-                if (!$catid) {
-                    print "<option value=\"0\">Choose a category</option>\n";
-                }
-                while (list($k, $v) = each($categories)) {
-                    print "<option value=\"" . $v['id'] . "\"";
-                    if ($v['id'] == $catid) {
-                        print " selected=\"selected\"";
-                    }
-                    print ">" . $v['title'] . "</option>\n";
-                }
-                ?>
-            </select></p>
-        <?php
-        if ($catid) {
-            ?>
-            <p>News source:<br/>
-                <select name="chanid">
-                    <?php
-                    if (!$chanid) {
-                        print "<option value=\"0\">Choose a source</option>\n";
-                    }
-                    while (list($k, $v) = each($sources)) {
-                        print "<option value=\"" . $v['id'] . "\"";
-                        if ($v['id'] == $chanid) {
-                            print "\" selected=\"selected\"";
-                        }
-                        print ">" . $v['title'] . "</option>\n";
-                    }
-                    ?>
-                </select>
-            </p>
-
-        <?php
-
-        } // end if ($catid)
-        ?>
-
-        <p><input type="submit" value="Update"/></p>
-        <input type="hidden" name="oc" value="<?php echo $catid;
-        ?>"/>
-    </form>
-
-    <?php
-    if ($chanid) {
-        ?>
-
-        <h2>Stories available</h2>
-        <table>
-            <?php
-            while (list($k, $v) = each($stories)) {
-                print "<tr>";
-                print "<td><b>" . $v['title'] . "</b><br />";
-                print $v['description'] . "<br />";
-                print "<em><a target=\"_blank\" href=\"" .
-                    $v['link'] . "\">Read full story</a> ";
-                print "<a href=\"comment.php?catid=${catid}&chanid=${chanid}&" .
-                    "oc=${oc}&comment=" . $v['id'] . "\">Comment on this story</a>";
-                print "</em>";
-                print "</td>";
-                print "</tr>\n";
-                // now look for existing comments
-                $res = dispatch($dclient, "discuss.getComments",
-                    array(new PhpXmlRpc\Value($v['id'])));
-                if (sizeof($res) > 0) {
-                    print "<tr><td bgcolor=\"#dddddd\"><p><b><i>" .
-                        "Comments on this story:</i></b></p>";
-                    for ($i = 0; $i < sizeof($res); $i++) {
-                        $s = $res[$i];
-                        print "<p><b>From:</b> " . htmlentities($s['name']) . "<br />";
-                        print "<b>Comment:</b> " . htmlentities($s['comment']) . "</p>";
-                    }
-                    print "</td></tr>\n";
-                }
-                print "<tr><td><hr /></td></tr>\n";
-            }
-            ?>
-        </table>
-
-    <?php
-
-    } // end if ($chanid)
-} // end if comment
-?>
-<hr/>
-<p>
-    <a href="http://meerkat.oreillynet.com"><img align="right"
-                                                 src="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg"
-                                                 height="31" width="88" alt="Meerkat powered, yeah!"
-                                                 border="0" hspace="8"/></a>
-</p>
-</body>
-</html>
similarity index 81%
rename from demo/client/client.php
rename to demo/client/getstatename.php
index 06a256a..69ce3e0 100644 (file)
@@ -5,7 +5,7 @@
 
 <h2>Send a U.S. state number to the server and get back the state name</h2>
 
-<h3>The code demonstrates usage of the php_xmlrpc_encode function</h3>
+<h3>The code demonstrates usage of automatic encoding/decoding of php variables into xmlrpc values</h3>
 <?php
 
 include_once __DIR__ . "/../../src/Autoloader.php";
@@ -24,9 +24,7 @@ if (isset($_POST["stateno"]) && $_POST["stateno"] != "") {
     if (!$r->faultCode()) {
         $v = $r->value();
         print "<br/>State number <b>" . $stateNo . "</b> is <b>"
-            . htmlspecialchars($v->scalarval()) . "</b><br/>";
-        // print "<HR>I got this value back<BR><PRE>" .
-        //  htmlentities($r->serialize()). "</PRE><HR>\n";
+            . htmlspecialchars($encoder->decode($v)) . "</b><br/>";
     } else {
         print "An error occurred: ";
         print "Code: " . htmlspecialchars($r->faultCode())
@@ -36,7 +34,7 @@ if (isset($_POST["stateno"]) && $_POST["stateno"] != "") {
     $stateNo = "";
 }
 
-print "<form action=\"client.php\" method=\"POST\">
+print "<form action=\"getstatename.php\" method=\"POST\">
 <input name=\"stateno\" value=\"" . $stateNo . "\"><input type=\"submit\" value=\"go\" name=\"submit\"></form>
 <p>Enter a state number to query its name</p>";
 
index 7200604..e11ac0e 100644 (file)
@@ -15,26 +15,31 @@ function display_error($r)
     print "Code: " . $r->faultCode()
         . " Reason: '" . $r->faultString() . "'<br/>";
 }
-// 'new style' client constructor
+
 $client = new PhpXmlRpc\Client("http://phpxmlrpc.sourceforge.net/server.php");
+
+// First off, let's retrieve the list of methods available on the remote server
 print "<h3>methods available at http://" . $client->server . $client->path . "</h3>\n";
 $req = new PhpXmlRpc\Request('system.listMethods');
 $resp = $client->send($req);
+
 if ($resp->faultCode()) {
     display_error($resp);
 } else {
     $v = $resp->value();
+
+    // Then, retrieve the signature and help text of each available method
     for ($i = 0; $i < $v->arraysize(); $i++) {
-        $mname = $v->arraymem($i);
-        print "<h4>" . $mname->scalarval() . "</h4>\n";
+        $methodName = $v->arraymem($i);
+        print "<h4>" . $methodName->scalarval() . "</h4>\n";
         // build messages first, add params later
         $m1 = new PhpXmlRpc\Request('system.methodHelp');
         $m2 = new PhpXmlRpc\Request('system.methodSignature');
-        $val = new PhpXmlRpc\Value($mname->scalarval(), "string");
+        $val = new PhpXmlRpc\Value($methodName->scalarval(), "string");
         $m1->addParam($val);
         $m2->addParam($val);
-        // send multiple messages in one pass.
-        // If server does not support multicall, client will fall back to 2 separate calls
+        // Send multiple requests in one http call.
+        // If server does not support multicall, client will automatically fall back to 2 separate calls
         $ms = array($m1, $m2);
         $rs = $client->send($ms);
         if ($rs[0]->faultCode()) {
@@ -52,13 +57,14 @@ if ($resp->faultCode()) {
             display_error($rs[1]);
         } else {
             print "<h4>Signature</h4><p>\n";
+            // note: using PhpXmlRpc\Encoder::decode() here would lead to cleaner code
             $val = $rs[1]->value();
             if ($val->kindOf() == "array") {
                 for ($j = 0; $j < $val->arraysize(); $j++) {
                     $x = $val->arraymem($j);
                     $ret = $x->arraymem(0);
                     print "<code>" . $ret->scalarval() . " "
-                        . $mname->scalarval() . "(";
+                        . $methodName->scalarval() . "(";
                     if ($x->arraysize() > 1) {
                         for ($k = 1; $k < $x->arraysize(); $k++) {
                             $y = $x->arraymem($k);
index e1966b3..9486e09 100644 (file)
@@ -10,9 +10,7 @@ if (isset($_GET['showSource']) && $_GET['showSource']) {
 <body>
 <h1>Mail demo</h1>
 
-<p>This form enables you to send mail via an XML-RPC server. For public use
-    only the "Userland" server will work (see <a href="http://www.xmlrpc.com/discuss/msgReader$598">Dave Winer's
-        message</a>).
+<p>This form enables you to send mail via an XML-RPC server.
     When you press <kbd>Send</kbd> this page will reload, showing you the XML-RPC request sent to the host server, the
     XML-RPC response received and the internal evaluation done by the PHP implementation.</p>
 
@@ -25,12 +23,8 @@ if (isset($_GET['showSource']) && $_GET['showSource']) {
 include_once __DIR__ . "/../../src/Autoloader.php";
 PhpXmlRpc\Autoloader::register();
 
-if (isset($_POST["server"]) && $_POST["server"]) {
-    if ($_POST["server"] == "Userland") {
-        $server = "http://206.204.24.2/RPC2";
-    } else {
-        $server = "http://pingu.heddley.com/xmlrpc/server.php";
-    }
+if (isset($_POST["mailto"]) && $_POST["mailto"]) {
+    $server = "http://phpxmlrpc.sourceforge.net/server.php";
     $req = new PhpXmlRpc\Request('mail.send', array(
         new PhpXmlRpc\Value($_POST["mailto"]),
         new PhpXmlRpc\Value($_POST["mailsub"]),
@@ -57,11 +51,6 @@ if (isset($_POST["server"]) && $_POST["server"]) {
 }
 ?>
 <form method="POST">
-    Server <select name="server">
-        <option value="Userland">Userland</option>
-        <option value="UsefulInc">UsefulInc private server</option>
-    </select>
-    <hr/>
     From <input size="60" name="mailfrom" value=""/><br/>
     <hr/>
     To <input size="60" name="mailto" value=""/><br/>
diff --git a/demo/client/proxy.php b/demo/client/proxy.php
new file mode 100644 (file)
index 0000000..2ffc0c4
--- /dev/null
@@ -0,0 +1,58 @@
+<html>
+<head><title>xmlrpc - Proxy demo</title></head>
+<body>
+<h1>proxy demo</h1>
+<h2>Query server using a 'proxy' object</h2>
+<h3>The code demonstrates usage for the terminally lazy</h3>
+<?php
+
+include_once __DIR__ . "/../../src/Autoloader.php";
+PhpXmlRpc\Autoloader::register();
+
+class PhpXmlRpcProxy
+{
+    protected $client;
+    protected $prefix = 'examples.';
+
+    public function __construct(PhpXmlRpc\Client $client)
+    {
+        $this->client = $client;
+    }
+
+    /**
+     * @author Toth Istvan
+     *
+     * @param string $name remote function name. Will be prefixed
+     * @param array $arguments
+     *
+     * @return mixed
+     *
+     * @throws Exception
+     */
+    function __call($name, $arguments)
+    {
+        $encoder = new PhpXmlRpc\Encoder();
+        $valueArray = array();
+        foreach ($arguments as $parameter) {
+            $valueArray[] = $encoder->encode($parameter);
+        }
+
+        // just in case this was set to something else
+        $this->client->return_type = 'phpvals';
+
+        $resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $valueArray));
+
+        if ($resp->faultCode()) {
+            throw new Exception($resp->faultMessage(), $resp->faultCode);
+        } else {
+            return $resp->value();
+        }
+    }
+
+}
+
+$stateNo = rand(1, 51);
+$proxy = new PhpXmlRpcProxy(new \PhpXmlRpc\Client('http://phpxmlrpc.sourceforge.net/server.php'));
+$stateName = $proxy->getStateName($stateNo);
+
+echo "State $stateNo is ".htmlspecialchars($stateName);
\ No newline at end of file
diff --git a/demo/client/simple_call.php b/demo/client/simple_call.php
deleted file mode 100644 (file)
index 4e904be..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-/**
- * Helper function for the terminally lazy.
- *
- * @copyright (c) 2006-2015 G. Giunta
- * @license code licensed under the BSD License: see file license.txt
- */
-
-include_once __DIR__ . "/../../src/Autoloader.php";
-PhpXmlRpc\Autoloader::register();
-
-/**
- * Takes a client object, a remote method name, and a variable numbers of
- * php values, and calls the method with the supplied parameters. The
- * parameters are native php values and the result is an xmlrpcresp object.
- *
- * Notes:
- * The function encodes the received parameters using php_xmlrpc_encode:
- * the limitations of automatic encoding apply to this function too);
- *
- * the type of the value returned by the function can be changed setting
- * beforehand the 'return_type' member of the client object to 'phpvals' -
- * see the manual for more details about this capability).
- *
- *
- * @author Toth Istvan
- *
- * @param xmlrpc_client client object, properly set up to connect to server
- * @param string remote function name
- * @param mixed $parameter1
- * @param mixed $parameter2
- * @param mixed $parameter3 ...
- *
- * @return xmlrpcresp or false on error
- */
-function xmlrpccall_simple()
-{
-    if (func_num_args() < 2) {
-        // Incorrect
-        return false;
-    } else {
-        $varargs = func_get_args();
-        $client = array_shift($varargs);
-        $remote_function_name = array_shift($varargs);
-        if (!is_a($client, 'xmlrpc_client') || !is_string($remote_function_name)) {
-            return false;
-        }
-
-        $valueArray = array();
-        $encoder = new PhpXmlRpc\Encoder();
-        foreach ($varargs as $parameter) {
-            $valueArray[] = $encoder->encode($parameter);
-        }
-
-        return $client->send(new PhpXmlRpc\Request($remote_function_name, $valueArray));
-    }
-}
index f1a7e00..4dde5ba 100644 (file)
@@ -5,9 +5,10 @@
 
 <h2>Wrap methods exposed by server into php functions</h2>
 
-<h3>The code demonstrates usage of the most automagic client usage possible:<br/>
+<h3>The code demonstrates usage of some the most automagic client usage possible:<br/>
     1) client that returns php values instead of xmlrpc value objects<br/>
-    2) wrapping of remote methods into php functions
+    2) wrapping of remote methods into php functions<br/>
+    See also proxy.php for an alternative take
 </h3>
 <?php
 
@@ -24,7 +25,7 @@ if ($resp->faultCode()) {
     $wrapper = new PhpXmlRpc\Wrapper();
     echo "<p>Server methods list retrieved, now wrapping it up...</p>\n<ul>\n";
     foreach ($resp->value() as $methodName) {
-        // $r->value is an array of strings
+        // $resp->value is an array of strings
 
         // do not wrap remote server system methods
         if (strpos($methodName, 'system.') !== 0) {
@@ -42,7 +43,7 @@ if ($resp->faultCode()) {
     echo "</ul>\n";
     if ($testCase) {
         echo "Now testing function $testCase: remote method to convert U.S. state number into state name";
-        $stateNum = 25;
+        $stateNum = rand(1, 51);
         $stateName = $testCase($stateNum, 2);
         echo "State number $stateNum is " . htmlspecialchars($stateName);
     }
diff --git a/demo/client/zopetest.php b/demo/client/zopetest.php
deleted file mode 100644 (file)
index 39e705e..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<html>
-<head><title>xmlrpc - Zope test demo</title></head>
-<body>
-<h1>Zope test demo</h1>
-
-<h3>The code demonstrates usage of basic authentication to connect to the server</h3>
-<?php
-
-include_once __DIR__ . "/../../src/Autoloader.php";
-PhpXmlRpc\Autoloader::register();
-
-$req = new PhpXmlRpc\Request('document_src', array());
-$client = new PhpXmlRpc\Client("pingu.heddley.com:9080/index_html");
-$client->setCredentials("username", "password");
-$client->setDebug(2);
-$resp = $client->send($req);
-if (!$resp->faultCode()) {
-    $value = $resp->value();
-    print "I received:" . htmlspecialchars($value->scalarval()) . "<br/>";
-    print "<hr/>I got this value back<br/>pre>" .
-        htmlentities($resp->serialize()) . "</pre>\n";
-} else {
-    print "An error occurred: ";
-    print "Code: " . htmlspecialchars($resp->faultCode())
-        . " Reason: '" . ($resp->faultString()) . "'<br/>";
-}
-?>
-</body>
-</html>
index b7a3871..01ed621 100644 (file)
@@ -18,19 +18,10 @@ class DemoFilesTest extends PhpXmlRpc_LocalFileTestCase
         $page = $this->request('client/agesort.php');
     }
 
-    public function testClient()
+    public function testGetStateName()
     {
-        $page = $this->request('client/client.php');
-
-        // we could test many more calls to the client demo, but the upstream server is gone anyway...
-
-        $page = $this->request('client/client.php', 'POST', array('stateno' => '1'));
-    }
-
-    public function testComment()
-    {
-        $page = $this->request('client/comment.php');
-        $page = $this->request('client/client.php', 'POST', array('storyid' => '1'));
+        $page = $this->request('client/getstatename.php');
+        $page = $this->request('client/getstatename.php', 'POST', array('stateno' => '1'));
     }
 
     public function testIntrospect()
@@ -41,8 +32,7 @@ class DemoFilesTest extends PhpXmlRpc_LocalFileTestCase
     public function testMail()
     {
         $page = $this->request('client/mail.php');
-        $page = $this->request('client/client.php', 'POST', array(
-            'server' => '',
+        $page = $this->request('client/mail.php', 'POST', array(
             "mailto" => '',
             "mailsub" => '',
             "mailmsg" => '',
@@ -52,9 +42,9 @@ class DemoFilesTest extends PhpXmlRpc_LocalFileTestCase
         ));
     }
 
-    public function testSimpleCall()
+    public function testProxy()
     {
-        $page = $this->request('client/simple_call.php', 'GET', null, true);
+        $page = $this->request('client/proxy.php', 'GET', null, true);
     }
 
     public function testWhich()
@@ -67,11 +57,6 @@ class DemoFilesTest extends PhpXmlRpc_LocalFileTestCase
         $page = $this->request('client/wrap.php');
     }
 
-    public function testZopeTest()
-    {
-        $page = $this->request('client/zopetest.php');
-    }
-
     public function testDiscussServer()
     {
         $page = $this->request('server/discuss.php');