--- /dev/null
+<?php
+require_once __DIR__ . "/_prepend.php";
+
+/**
+ * Demoing the charset conversion of the library: create a client class which uses data in the CP-1252 character set,
+ * regardless of the character set used by the server
+ */
+
+use PhpXmlRpc\Client;
+use PhpXmlRpc\PhpXmlRpc;
+use PhpXmlRpc\Request;
+use PhpXmlRpc\Value;
+
+PhpXmlRpc::$xmlrpc_internalencoding = 'Windows-1252';
+
+// this is a very contrived way of creating a CP-1252 string...
+$input = 'Euro sign is €, per mille is ‰, trademark is ™, copyright is ©, smart quotes are “these”';
+//var_dump(mb_list_encodings());
+echo "This is the value we start with (in UTF-8): ";
+var_dump($input);
+
+$input = mb_convert_encoding($input, 'Windows-1252', 'UTF-8');
+
+echo "In CP-1252, it looks like this: ";
+var_dump($input);
+
+$c = new Client(XMLRPCSERVER);
+
+// allow the full request and response to be seen on screen
+$c->setDebug(2);
+// tell the server not to compress the response
+$c->accepted_compression = array();
+// tell the server not to encode everything as ASCII - this is not necessary btw. It is only done to make the demo nicer
+$c->accepted_charset_encodings = array('UTF-8');
+// force the client not to encode everything as ASCII - this is not necessary btw. It is only done to make the demo nicer
+$c->request_charset_encoding = 'UTF-8';
+
+$r = $c->send(new Request('examples.stringecho', array(new Value($input))));
+$output = $r->value()->scalarval();
+
+echo "This is the value we got back from the server (in CP-1252): ";
+var_dump($output);
+
+echo "In UTF-8, it looks like this: ";
+var_dump(mb_convert_encoding($output, 'UTF-8', 'Windows-1252'));
<h3>The code demonstrates usage of some of the most automagic client usage possible:<br/>
1) client that returns php values instead of xml-rpc Value objects<br/>
2) wrapping of remote methods into php functions<br/>
- See also proxy.php for an alternative take
+ See also proxy.php and codegen.php for alternative takes
</h3>
<p>You can see the source to this page here: <a href="wrap.php?showSource=1">wrap.php</a></p>
');
$getComments_doc = 'Returns an array of comments for a given ID, which is the sole argument. Each array item is a struct ' .
'containing name and comment text.';
-$srv = new Server(array(
+$srv = new Server();
+
+$srv->setDispatchMap(array(
"discuss.addComment" => array(
"function" => array($manager, "addComment"),
"signature" => $addComment_sig,
"signature" => $getComments_sig,
"docstring" => $getComments_doc,
),
-), false);
+));
-// let the xml=rpc server know that the method-handler functions expect plain php values
+// let the xml-rpc server know that the method-handler functions expect plain php values
$srv->functions_parameters_type = 'phpvals';
// let code exceptions float all the way to the remote caller as xml-rpc faults - it helps debugging
*/
use PhpXmlRpc\Encoder;
+use PhpXmlRpc\PhpXmlRpc;
use PhpXmlRpc\Response;
use PhpXmlRpc\Server;
use PhpXmlRpc\Value;
if ($err != '') {
// if we generated an error, create an error return response
- return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+ return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
} else {
// otherwise, we create the right response with the state name
return new Response(new Value($stateName));
if ($err != '') {
Server::xmlrpc_debugmsg("Aborting 'agesorter'");
- return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+ return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
}
asort($agar);
public static $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload';
public static function echoBack($req)
{
- // just sends back a string with what I got sent to me, that's all (escaping for xml is automatic)
- $s = "I got the following message:\n" . $req->serialize();
+ // just sends back a string with what I got sent to me, that's all
+
+ /// @todo file_get_contents does not take into account either receiving compressed requests, or requests with
+ /// data which is not in UTF-8. Otoh using req->serialize means that what we are sending back is not
+ /// byte-for-byte identical to what we received, and that <, >, ', " and & will be double-encoded.
+ /// In fact, we miss some API (or extra data) in the Request...
+ //$payload = file_get_contents('php://input');
+ $payload = $req->serialize(PhpXmlRpc::$xmlrpc_internalencoding);
+ $s = "I got the following message:\n" . $payload;
return new Response(new Value($s));
}
}
if ($err) {
- return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
+ return new Response(0, PhpXmlRpc::$xmlrpcerruser, $err);
} else {
return new Response(new Value(true, Value::$xmlrpcBoolean));
}
$client = new Client($url);
if ($req->getNumParams() > 3) {
- // we have to set some options onto the client.
+ // We have to set some options onto the client.
// Note that if we do not untaint the received values, warnings might be generated...
$options = $encoder->decode($req->getParam(3));
foreach ($options as $key => $val) {
switch ($key) {
- case 'Cookie':
+ case 'authType':
+ /// @todo add support for this if needed
+ break;
+ case 'followRedirects':
+ // requires cURL to be enabled
+ if ($val) {
+ $client->use_curl = Client::USE_CURL_ALWAYS;
+ $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_POSTREDIR => 3));
+ }
+ case 'Cookies':
/// @todo add support for this if needed
break;
case 'Credentials':
/// @todo add support for this as well if needed
break;
+ case 'HTTPProxy':
+ case 'HTTPProxyCredentials':
+ /// @todo add support for this as well if needed
+ break;
+ case 'RequestCharsetEncoding':
+ // allow the server to work as charset transcoder.
+ // NB: works best with mbstring enabled
+ $client->request_charset_encoding = $val;
+ break;
case 'RequestCompression':
$client->setRequestCompression($val);
break;
$v = new PhpXmlRpc\Value('κόσμε');
output("Greek (default encoding): <PRE>" . htmlentities($v->serialize()) . "</PRE>");
output("Greek (utf8 encoding): <PRE>" . htmlentities($v->serialize('UTF-8')) . "</PRE>");
+if (function_exists('mb_convert_encoding')) {
+ output("Greek (ISO-8859-7 encoding): <PRE>" . htmlentities($v->serialize('ISO-8859-7')) . "</PRE>");
+}
output("<h3>Testing request serialization</h3>\n");
$req = new PhpXmlRpc\Request('examples.getStateName');