Skip to content

Commit

Permalink
Refactor demo files (client side): remove redundant ones; make sure a…
Browse files Browse the repository at this point in the history
…ll use existing servers; add a proxy example
  • Loading branch information
gggeek committed May 12, 2015
1 parent a518f17 commit 92e61c9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 348 deletions.
211 changes: 0 additions & 211 deletions demo/client/comment.php

This file was deleted.

8 changes: 3 additions & 5 deletions demo/client/client.php → demo/client/getstatename.php
Expand Up @@ -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";
Expand All @@ -24,9 +24,7 @@
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())
Expand All @@ -36,7 +34,7 @@
$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>";

Expand Down
20 changes: 13 additions & 7 deletions demo/client/introspect.php
Expand Up @@ -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()) {
Expand All @@ -52,13 +57,14 @@ function display_error($r)
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);
Expand Down
17 changes: 3 additions & 14 deletions demo/client/mail.php
Expand Up @@ -10,9 +10,7 @@
<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>

Expand All @@ -25,12 +23,8 @@
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"]),
Expand All @@ -57,11 +51,6 @@
}
?>
<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/>
Expand Down
58 changes: 58 additions & 0 deletions demo/client/proxy.php
@@ -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);

0 comments on commit 92e61c9

Please sign in to comment.