proposal for extending XML/RPC.php
| From: | Sebastien Person | Date: | Wed, 10 Mar 2004 09:40:12 +0000 |
| Subject: | proposal for extending XML/RPC.php | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-26248@lists.php.net to get a copy of this message | ||
Hi,
I'm new to the pear-dev list so I'm not very familiar with the habits and I hope
that I won't break any rules.
I use the XML-RPC functions of the pear lib. I saw that there were possibility
to ask xml webservices for documentation and signature string.
If the XML_RPC_Client class could provide an easy way to find all available
method from a web service and also provide it's documentation and signature it
would be an interesting function for developpers involving with xml rpc.
So I added a discover() method to this class wich when called, print out all
these informations. Maybe this could interest some other people so I post it to
the list.
You'll find enclosed two files. The first contains the 3 functions to add to the
XML_RPC_Client class :
/**
* private : list parameters for a given signature
*/
function sig_details($sig)
{
if ($sig->KindOf() == "array"){
echo "<pre style=\"background-color:#cccccc;\">";
// loop throught all available parameters
$number_of_param = $sig->arraysize();
for ($i=0 ; $i<$number_of_param; $i++){
$param = $sig->arraymem($i);
$param_scalarval = $param->scalarval();
if ($i == 0)
echo "\nouput : " . $param_scalarval;
else
echo "\ninput : " . $param_scalarval;
}
echo "</pre>";
}
}
/**
* private : details (doc and signature) for a given method
*/
function method_details($method)
{
// request method doc
$doc_response = $this->send(new XML_RPC_Message("system.methodHelp",
array($method)));
// request method signature
$sig_response = $this->send(new
XML_RPC_Message("system.methodSignature",
array($method)));
// be sure there isn't any I/O trouble
if ($doc_response == 0 || $sig_response == 0) {
echo "<b>Error occured :</b> " . $this->errno() .
$this->errstring(); return;
}
if ($doc_response->faultcode()) {
$doc_response->faultString();
return ;
}
// extract
$method_name = $method->scalarval();
// doc stuff
$doc_value = $doc_response->value();
$doc_str = $doc_value->scalarval();
echo "<hr><pre><b>" . $method_name .
"</b></pre>". $doc_str;
// signature stuff
$sig_value = $sig_response->value();
if ($sig_value->KindOf() == "array"){
// loop throught all available signature
$number_of_sig = $sig_value->arraysize();
for ($i=0 ; $i<$number_of_sig; $i++){
$sig = $sig_value->arraymem($i);
$this->sig_details($sig);
}
}
}
/**
* public : discover all client methods (doc and signatures)
*/
function discover()
{
$response = $this->send(new XML_RPC_Message('system.listMethods'));
if(!$response->faultcode()){
$method_value = $response->value();
// check if the object is an "array"
if ($method_value->KindOf() == "array"){
// loop throught all method
$number_of_methods = $method_value->arraysize();
for ( $i= 0; $i < $number_of_methods; $i++){
$method = $method_value->arraymem($i);
$this->method_details($method);
}
}
}
}
--------------8<---------------------8<---------------
and here follows a simple example of use :
<?php
require_once("XML/RPC.php"); // pear client libs
// here change webservices and localhost to fit your need
$client = new XML_RPC_Client("/webservices.php", "localhost", 80);
// follow an example on a public server
//$client = new XML_RPC_Client("/meerkat/xml-rpc/server.php",
"www.oreillynet.com", 80);$client->setdebug(0);
$client->discover();
?>
I didn't know if this modifications follow all rules of the pear lib, but
don't hesitate to let me know how this could be improve or if it could be
integrated to pear (if it respond to a need).
Regards,
seb