Don't like ads? PRO users don't see any ads ;-)
Guest

XML-RPC Client Wrapper

By: katiek on Dec 21st, 2011  |  syntax: PHP  |  size: 3.72 KB  |  hits: 104  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Based on PHP XML-RPC http://phpxmlrpc.sourceforge.net/
  2. require_once("run/xmlrpc-300-beta/xmlrpc.inc");
  3.  
  4. /**
  5.  * Bare-bones RPC caller, which uses XML-RPC and provides
  6.  * functionality for "disconnection" when the server is down but the
  7.  * testing must go on.
  8.  * Good if considering retrieving data in other ways in the future, but using
  9.  * XML-RPC for now.
  10.  */
  11. class RPC {
  12.  
  13.     private $hostName = "localhost";
  14.     private $serverName = null;
  15.     private $portNumber = -1;
  16.     private $debug = 0;
  17.     private $disconnected = false;
  18.     private $dummyfunction = false;
  19.  
  20.     /**
  21.      * Create a new connectio nto the data server by specifying the server name and the port number.
  22.      * The hostname can also be specified, but is assumed to be localhost otherwise.
  23.      * @param string $serverName name of XML-RPC server
  24.      * @param int $portNumber the port that the server is listening on
  25.      * @param string $hostName Default value is localhost
  26.      */
  27.     function __construct($serverName, $portNumber, $hostName='localhost') {
  28.         $this->serverName = $serverName;
  29.         $this->portNumber = $portNumber;
  30.         $this->hostName = $hostName;
  31.         $this->debug = 0;
  32.         $this->disconnected = false;
  33.         $this->dummyfunction = null;
  34.     }
  35.  
  36.     /**
  37.      * If the argument is numeric, the internal, static debug variable
  38.      * will be set to this number. 0 (and less) is silent; anything above
  39.      * that will get ccarional output, and the number will also be passed
  40.      * to setDebug in the PHP XML-RPC call, where the max verbosity is at 3.
  41.      * @param type $amt
  42.      */
  43.     function debug($amt) {
  44.         if (is_numeric($amt))
  45.             $this->debug = $amt;
  46.     }
  47.  
  48.     /**
  49.      * Disconnects the client - no server call is made even if the script
  50.      * is used, but the user-defined function by name of $func is called
  51.      * with $method, $data that one normally calls RPC::get() with. The
  52.      * get() method will also return anythign that the user-defined
  53.      * function returns.
  54.      * @param string $dummyfunction must be the name of a function that serves
  55.      * data similar to what the server would otherwise provide.
  56.      */
  57.     function disconnect($dummyfunction) {
  58.         $this->disconnected = true;
  59.         $this->dummyfunction = $dummyfunction;
  60.         if ($this->debug > 0)
  61.             echo "RPC disconnected, re-routed to <em>$dummyfunction</em>\n";
  62.     }
  63.  
  64.     /**
  65.      * Makes a call, unless this has been disconnected (see disconnect(string)),
  66.      * in which case just calls the provided user-defined function with the same parameters as here.
  67.      * @param string $method the name of the method to call on the server
  68.      * @param array $data an array of values - these must be cast to what the PHP XML RPC encoder needs to know them as!
  69.      * @return mixed Whetever the server returns, having been wrung through the PHP XML RPC decoder
  70.      */
  71.     function get($method, $data=array()) {
  72.         if ($this->disconnected) {
  73.             return call_user_func($this->dummyfunction, $method, $data);
  74.         } else {
  75.             if ($data != null) {
  76.                 if (!is_array($data)) {
  77.                     $data = array($data);
  78.                 }
  79.                 for ($i = 0; $i < count($data); $i++) {
  80.                     $data[$i] = php_xmlrpc_encode($data[$i]);
  81.                 }
  82.             }
  83.             $f = new xmlrpcmsg($this->serverName . "." . $method, $data);
  84.             $c = new xmlrpc_client("/xmlrpc", $this->hostName, $this->portNumber);
  85.             $c->setDebug($this->debug);
  86.             $r = &$c->send($f);
  87.             if (!$r->faultCode()) {
  88.                 return php_xmlrpc_decode($r->value());
  89.             }
  90.         }
  91.         return "";
  92.     }
  93.  
  94. }