getParam(0); // param must be there and of the correct type: server object does the validation for us // extract the value of the state number $snv = $sno->scalarval(); // look it up in our array (zero-based) if (isset($GLOBALS['stateNames'][$snv - 1])) { $stateName = $GLOBALS['stateNames'][$snv - 1]; } else { // not there, so complain $err = "I don't have a state for the index '" . $snv . "'"; } // if we generated an error, create an error return response if ($err) { return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { // otherwise, we create the right response with the state name return new Response(new Value($stateName)); } } // Sorting demo // // send me an array of structs thus: // // Dave 35 // Edd 45 // Fred 23 // Barney 37 // // and I'll return it to you in sorted order function agesorter_compare($a, $b) { /// @todo move away from usage of globals for such a simple case global $agesorter_arr; // don't even ask me _why_ these come padded with hyphens, I couldn't tell you :p $a = str_replace("-", "", $a); $b = str_replace("-", "", $b); if ($agesorter_arr[$a] == $agesorter_arr[$b]) { return 0; } return ($agesorter_arr[$a] > $agesorter_arr[$b]) ? -1 : 1; } $agesorter_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)); $agesorter_doc = 'Send this method an array of [string, int] structs, eg:
 Dave   35
 Edd    45
 Fred   23
 Barney 37
And the array will be returned with the entries sorted by their numbers. '; function ageSorter($req) { global $agesorter_arr; Server::xmlrpc_debugmsg("Entering 'agesorter'"); // get the parameter $sno = $req->getParam(0); // error string for [if|when] things go wrong $err = ""; $agar = array(); $max = $sno->count(); Server::xmlrpc_debugmsg("Found $max array elements"); foreach ($sno as $i => $rec) { if ($rec->kindOf() != "struct") { $err = "Found non-struct in array at element $i"; break; } // extract name and age from struct $n = $rec["name"]; $a = $rec["age"]; // $n and $a are Values, // so get the scalarval from them $agar[$n->scalarval()] = $a->scalarval(); } // create the output value $v = new Value(array(), Value::$xmlrpcArray); $agesorter_arr = $agar; // hack, must make global as uksort() won't // allow us to pass any other auxiliary information uksort($agesorter_arr, 'agesorter_compare'); foreach($agesorter_arr as $key => $val) { // recreate each struct element $v[] = new Value( array( "name" => new Value($key), "age" => new Value($val, "int") ), Value::$xmlrpcStruct ); } if ($err) { return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { return new Response($v); } } $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt)); $addtwo_doc = 'Add two integers together and return the result'; function addTwo($req) { $s = $req->getParam(0); $t = $req->getParam(1); return new Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcInt)); } $addtwodouble_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble, Value::$xmlrpcDouble)); $addtwodouble_doc = 'Add two doubles together and return the result'; function addTwoDouble($req) { $s = $req->getParam(0); $t = $req->getParam(1); return new Response(new Value($s->scalarval() + $t->scalarval(), Value::$xmlrpcDouble)); } $stringecho_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString)); $stringecho_doc = 'Accepts a string parameter, returns the string.'; function stringEcho($req) { // just sends back a string return new Response(new Value($req->getParam(0)->scalarval())); } $echoback_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString)); $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload'; function echoBack($req) { // just sends back a string with what i got sent to me, just escaped, that's all $s = "I got the following message:\n" . $req->serialize(); return new Response(new Value($s)); } $echosixtyfour_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcBase64)); $echosixtyfour_doc = 'Accepts a base64 parameter and returns it decoded as a string'; function echoSixtyFour($req) { // Accepts an encoded value, but sends it back as a normal string. // This is to test that base64 encoding is working as expected $incoming = $req->getParam(0); return new Response(new Value($incoming->scalarval(), Value::$xmlrpcString)); } $bitflipper_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)); $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted'; function bitFlipper($req) { $v = $req->getParam(0); $rv = new Value(array(), Value::$xmlrpcArray); foreach ($v as $b) { if ($b->scalarval()) { $rv[] = new Value(false, Value::$xmlrpcBoolean); } else { $rv[] = new Value(true, Value::$xmlrpcBoolean); } } return new Response($rv); } $getallheaders_sig = array(array(Value::$xmlrpcStruct)); $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS'; function getAllHeaders_xmlrpc($req) { $encoder = new Encoder(); if (function_exists('getallheaders')) { return new Response($encoder->encode(getallheaders())); } else { $headers = array(); // IIS: poor man's version of getallheaders foreach ($_SERVER as $key => $val) { if (strpos($key, 'HTTP_') === 0) { $key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5)))); $headers[$key] = $val; } } return new Response($encoder->encode($headers)); } } $setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct)); $setcookies_doc = 'Sends to client a response containing a single \'1\' digit, and sets to it http cookies as received in the request (array of structs describing a cookie)'; function setCookies($req) { $encoder = new Encoder(); $cookies = $req->getParam(0); foreach ($cookies as $name => $value) { $cookieDesc = $encoder->decode($value); setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']); } return new Response(new Value(1, Value::$xmlrpcInt)); } $getcookies_sig = array(array(Value::$xmlrpcStruct)); $getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)'; function getCookies($req) { $encoder = new Encoder(); return new Response($encoder->encode($_COOKIE)); } $mailsend_sig = array(array( Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, )); $mailsend_doc = 'mail.send(recipient, subject, text, sender, cc, bcc, mimetype)
recipient, cc, and bcc are strings, comma-separated lists of email addresses, as described above.
subject is a string, the subject of the message.
sender is a string, it\'s the email address of the person sending the message. This string can not be a comma-separated list, it must contain a single email address only.
text is a string, it contains the body of the message.
mimetype, a string, is a standard MIME type, for example, text/plain. '; // WARNING; this functionality depends on the sendmail -t option // it may not work with Windows machines properly; particularly // the Bcc option. Sneak on your friends at your own risk! function mailSend($req) { $err = ""; $mTo = $req->getParam(0); $mSub = $req->getParam(1); $mBody = $req->getParam(2); $mFrom = $req->getParam(3); $mCc = $req->getParam(4); $mBcc = $req->getParam(5); $mMime = $req->getParam(6); if ($mTo->scalarval() == "") { $err = "Error, no 'To' field specified"; } if ($mFrom->scalarval() == "") { $err = "Error, no 'From' field specified"; } $msgHdr = "From: " . $mFrom->scalarval() . "\n"; $msgHdr .= "To: " . $mTo->scalarval() . "\n"; if ($mCc->scalarval() != "") { $msgHdr .= "Cc: " . $mCc->scalarval() . "\n"; } if ($mBcc->scalarval() != "") { $msgHdr .= "Bcc: " . $mBcc->scalarval() . "\n"; } if ($mMime->scalarval() != "") { $msgHdr .= "Content-type: " . $mMime->scalarval() . "\n"; } $msgHdr .= "X-Mailer: XML-RPC for PHP mailer 1.0"; if ($err == "") { if (!mail("", $mSub->scalarval(), $mBody->scalarval(), $msgHdr) ) { $err = "Error, could not send the mail."; } } if ($err) { return new Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { return new Response(new Value(true, Value::$xmlrpcBoolean)); } } return array( "examples.getStateName" => array( "function" => "findState", "signature" => $findstate_sig, "docstring" => $findstate_doc, ), "examples.sortByAge" => array( "function" => "ageSorter", "signature" => $agesorter_sig, "docstring" => $agesorter_doc, ), "examples.addtwo" => array( "function" => "addTwo", "signature" => $addtwo_sig, "docstring" => $addtwo_doc, ), "examples.addtwodouble" => array( "function" => "addTwoDouble", "signature" => $addtwodouble_sig, "docstring" => $addtwodouble_doc, ), "examples.stringecho" => array( "function" => "stringEcho", "signature" => $stringecho_sig, "docstring" => $stringecho_doc, ), "examples.echo" => array( "function" => "echoBack", "signature" => $echoback_sig, "docstring" => $echoback_doc, ), "examples.decode64" => array( "function" => "echoSixtyFour", "signature" => $echosixtyfour_sig, "docstring" => $echosixtyfour_doc, ), "examples.invertBooleans" => array( "function" => "bitFlipper", "signature" => $bitflipper_sig, "docstring" => $bitflipper_doc, ), "examples.getallheaders" => array( "function" => 'getAllHeaders_xmlrpc', "signature" => $getallheaders_sig, "docstring" => $getallheaders_doc, ), "examples.setcookies" => array( "function" => 'setCookies', "signature" => $setcookies_sig, "docstring" => $setcookies_doc, ), "examples.getcookies" => array( "function" => 'getCookies', "signature" => $getcookies_sig, "docstring" => $getcookies_doc, ), "mail.send" => array( "function" => "mailSend", "signature" => $mailsend_sig, "docstring" => $mailsend_doc, ), );