RPC Service

RPC Service stands for "Remote Procedure Call" Service and is a common name used to identify a service that exposes an interface that can be called by remote programs in order to execute a procedure. An RPC Service tipically exposes one or more APIs (Application programming interface), permitting remote clients to make requests to the available methods and receive a proper response. The interface itself is not bound to a specific programming language, but uses a standard data exchange protocol (tipically xml or json). PRADO provides TRpcService that makes developing a RPC server application an extremely easy task.

To use TRpcService, configure it in the application specification like following:

<services>
  <service id="rpc" class="System.Web.Services.TRpcService">
    <rpcapi id="stockquote" class="path.to.StockQuote" />
    <!--
    <rpcapi...other api... />
    -->
  </service>
</services>

The example specifies a RPC service provider named stockquote which implements the getPrice RPC method in the provider class StockQuote,

class StockQuote
{
    /**
     * @param string $symbol the symbol of the stock
     * @return float the stock price
     * @soapmethod
     */
    public function getPrice($symbol)
    {
        ....return stock price for $symbol
    }
 
    /**
     * Register the available methods
     * @return the list of implemented methods
     */
    public function registerMethods()
    {
        return array(
        'getPrice' => array('method' => array($this, 'getPrice')),
        );
    }
 
}

PRADO already bundles two common protocols: TXmlRpcService and TJsonRpcService: requests made by clients using one of these protocol will be automatically resolved and will call the user-specified APIs.

Note: TXmlRpcService is based on PHP XML-RPC extension and thus requires the extension to be installed.

With the above simple code, we already finish a simple RPC service that allows other applications to query the price of a specific stock. A client needs to know the exact url of the service, the name of the method and the list of parameters needed by the method.

// Assuming we're using the json-rpc php library from http://jsonrpcphp.org/
 
require_once 'jsonRPCClient.php'; 
$client=new jsonRPCClient('http://path/to/index.php?rpc=stockquote');
echo $client->getPrice('IBM');