From 92e61c97411b8652b235e9c05d64934d83c53671 Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 12 May 2015 23:49:19 +0100 Subject: [PATCH] Refactor demo files (client side): remove redundant ones; make sure all use existing servers; add a proxy example --- demo/client/comment.php | 211 ------------------- demo/client/{client.php => getstatename.php} | 8 +- demo/client/introspect.php | 20 +- demo/client/mail.php | 17 +- demo/client/proxy.php | 58 +++++ demo/client/simple_call.php | 57 ----- demo/client/wrap.php | 9 +- demo/client/zopetest.php | 29 --- tests/5DemofilesTest.php | 27 +-- 9 files changed, 88 insertions(+), 348 deletions(-) delete mode 100644 demo/client/comment.php rename demo/client/{client.php => getstatename.php} (81%) create mode 100644 demo/client/proxy.php delete mode 100644 demo/client/simple_call.php delete mode 100644 demo/client/zopetest.php diff --git a/demo/client/comment.php b/demo/client/comment.php deleted file mode 100644 index 2c34d3f..0000000 --- a/demo/client/comment.php +++ /dev/null @@ -1,211 +0,0 @@ -"; - exit(); -} - -function dispatch($client, $method, $args) -{ - $req = new PhpXmlRpc\Request($method, $args); - $resp = $client->send($req); - if (!$resp) { - print "

IO error: " . $client->errstr . "

"; - bomb(); - } - if ($resp->faultCode()) { - print "

There was an error: " . $resp->faultCode() . " " . - $resp->faultString() . "

"; - 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 - -?> - -meerkat browser - -

Meerkat integration

- -

Make a comment on the story

-
-

Your name:

- -

Your comment:

- - "/> - - - -
- 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"))); - } - ?> -
-

Subject area:
-

- -

News source:
- -

- - - -

- -
- - - -

Stories available

- - "; - print ""; - print "\n"; - // now look for existing comments - $res = dispatch($dclient, "discuss.getComments", - array(new PhpXmlRpc\Value($v['id']))); - if (sizeof($res) > 0) { - print "\n"; - } - print "\n"; - } - ?> -
" . $v['title'] . "
"; - print $v['description'] . "
"; - print "Read full story "; - print "Comment on this story"; - print ""; - print "

" . - "Comments on this story:

"; - for ($i = 0; $i < sizeof($res); $i++) { - $s = $res[$i]; - print "

From: " . htmlentities($s['name']) . "
"; - print "Comment: " . htmlentities($s['comment']) . "

"; - } - print "

- - -
-

- Meerkat powered, yeah! -

- - diff --git a/demo/client/client.php b/demo/client/getstatename.php similarity index 81% rename from demo/client/client.php rename to demo/client/getstatename.php index 06a256a..69ce3e0 100644 --- a/demo/client/client.php +++ b/demo/client/getstatename.php @@ -5,7 +5,7 @@

Send a U.S. state number to the server and get back the state name

-

The code demonstrates usage of the php_xmlrpc_encode function

+

The code demonstrates usage of automatic encoding/decoding of php variables into xmlrpc values

faultCode()) { $v = $r->value(); print "
State number " . $stateNo . " is " - . htmlspecialchars($v->scalarval()) . "
"; - // print "
I got this value back
" .
-        //  htmlentities($r->serialize()). "

\n"; + . htmlspecialchars($encoder->decode($v)) . "
"; } else { print "An error occurred: "; print "Code: " . htmlspecialchars($r->faultCode()) @@ -36,7 +34,7 @@ if (isset($_POST["stateno"]) && $_POST["stateno"] != "") { $stateNo = ""; } -print "
+print "

Enter a state number to query its name

"; diff --git a/demo/client/introspect.php b/demo/client/introspect.php index 7200604..e11ac0e 100644 --- a/demo/client/introspect.php +++ b/demo/client/introspect.php @@ -15,26 +15,31 @@ function display_error($r) print "Code: " . $r->faultCode() . " Reason: '" . $r->faultString() . "'
"; } -// '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 "

methods available at http://" . $client->server . $client->path . "

\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 "

" . $mname->scalarval() . "

\n"; + $methodName = $v->arraymem($i); + print "

" . $methodName->scalarval() . "

\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 "

Signature

\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 "" . $ret->scalarval() . " " - . $mname->scalarval() . "("; + . $methodName->scalarval() . "("; if ($x->arraysize() > 1) { for ($k = 1; $k < $x->arraysize(); $k++) { $y = $x->arraymem($k); diff --git a/demo/client/mail.php b/demo/client/mail.php index e1966b3..9486e09 100644 --- a/demo/client/mail.php +++ b/demo/client/mail.php @@ -10,9 +10,7 @@ if (isset($_GET['showSource']) && $_GET['showSource']) {

Mail demo

-

This form enables you to send mail via an XML-RPC server. For public use - only the "Userland" server will work (see Dave Winer's - message). +

This form enables you to send mail via an XML-RPC server. When you press Send 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.

@@ -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"]) { } ?>
- Server -
From

To
diff --git a/demo/client/proxy.php b/demo/client/proxy.php new file mode 100644 index 0000000..2ffc0c4 --- /dev/null +++ b/demo/client/proxy.php @@ -0,0 +1,58 @@ + +xmlrpc - Proxy demo + +

proxy demo

+

Query server using a 'proxy' object

+

The code demonstrates usage for the terminally lazy

+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 index 4e904be..0000000 --- a/demo/client/simple_call.php +++ /dev/null @@ -1,57 +0,0 @@ -encode($parameter); - } - - return $client->send(new PhpXmlRpc\Request($remote_function_name, $valueArray)); - } -} diff --git a/demo/client/wrap.php b/demo/client/wrap.php index f1a7e00..4dde5ba 100644 --- a/demo/client/wrap.php +++ b/demo/client/wrap.php @@ -5,9 +5,10 @@

Wrap methods exposed by server into php functions

-

The code demonstrates usage of the most automagic client usage possible:
+

The code demonstrates usage of some the most automagic client usage possible:
1) client that returns php values instead of xmlrpc value objects
- 2) wrapping of remote methods into php functions + 2) wrapping of remote methods into php functions
+ See also proxy.php for an alternative take

faultCode()) { $wrapper = new PhpXmlRpc\Wrapper(); echo "

Server methods list retrieved, now wrapping it up...

\n
    \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 "
\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 index 39e705e..0000000 --- a/demo/client/zopetest.php +++ /dev/null @@ -1,29 +0,0 @@ - -xmlrpc - Zope test demo - -

Zope test demo

- -

The code demonstrates usage of basic authentication to connect to the server

-setCredentials("username", "password"); -$client->setDebug(2); -$resp = $client->send($req); -if (!$resp->faultCode()) { - $value = $resp->value(); - print "I received:" . htmlspecialchars($value->scalarval()) . "
"; - print "
I got this value back
pre>" . - htmlentities($resp->serialize()) . "\n"; -} else { - print "An error occurred: "; - print "Code: " . htmlspecialchars($resp->faultCode()) - . " Reason: '" . ($resp->faultString()) . "'
"; -} -?> - - diff --git a/tests/5DemofilesTest.php b/tests/5DemofilesTest.php index b7a3871..01ed621 100644 --- a/tests/5DemofilesTest.php +++ b/tests/5DemofilesTest.php @@ -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'); -- 2.43.0