* @link http://www.pradosoft.com/ * @copyright 2010 Bigpoint GmbH * @license http://www.pradosoft.com/license/ * @since 3.2 * @package Prado\Util */ namespace Prado\Util; /** * TRpcClient class * * Note: When using setIsNotification(true), *every* following request is also * considered to be a notification until you use setIsNotification(false). * * Usage: * * First, you can use the factory: *
 * $_rpcClient = TRpcClient::create('xml', 'http://host/server');
 * $_result = $_rpcClient->remoteMethodName($param, $otherParam);
 * 
* * or as oneliner: *
 * $_result = TRpcClient::create('json', 'http://host/server')->remoteMethod($param, ...);
 * 
* * Second, you can also use the specific implementation directly: *
 * $_rpcClient = new TXmlRpcClient('http://host/server');
 * $_result = $_rpcClient->remoteMethod($param, ...);
 * 
* * or as oneliner: *
 * $_result = TXmlRpcClient('http://host/server')->hello();
 * 
* * @author Robin J. Rogge * @version $Id$ * @package Prado\Util * @since 3.2 */ class TRpcClient extends TApplicationComponent { /** * @var string url of the RPC server */ private $_serverUrl; /** * @var boolean whether the request is a notification and therefore should not care about the result (default: false) */ private $_isNotification = false; // magics /** * @param string url to RPC server * @param boolean whether requests are considered to be notifications (completely ignoring the response) (default: false) */ public function __construct($serverUrl, $isNotification = false) { $this->_serverUrl = $serverUrl; $this->_isNotification = TPropertyValue::ensureBoolean($isNotification); } // methods /** * Creates an instance of the requested RPC client type * @return TRpcClient instance * @throws TApplicationException if an unsupported RPC client type was specified */ public static function create($type, $serverUrl, $isNotification = false) { if(($_handler = constant('TRpcClientTypesEnumerable::'.strtoupper($type))) === null) throw new TApplicationException('rpcclient_unsupported_handler'); return new $_handler($serverUrl, $isNotification); } /** * Creates a stream context resource * @param mixed $content * @param string $contentType mime type */ protected function createStreamContext($content, $contentType) { return stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => "Content-Type: {$contentType}", 'content' => $content ) )); } /** * Performs the actual request * @param string RPC server URL * @param array payload data * @param string request mime type */ protected function performRequest($serverUrl, $payload, $mimeType) { if(($_response = @file_get_contents($serverUrl, false, $this->createStreamContext($payload, $mimeType))) === false) throw new TRpcClientRequestException('Request failed ("'.$http_response_header[0].'")'); return $_response; } // getter/setter /** * @return boolean whether requests are considered to be notifications (completely ignoring the response) */ public function getIsNotification() { return $this->_isNotification; } /** * @param string boolean whether the requests are considered to be notifications (completely ignoring the response) (default: false) */ public function setIsNotification($bool) { $this->_isNotification = TPropertyValue::ensureBoolean($bool); } /** * @return string url of the RPC server */ public function getServerUrl() { return $this->_serverUrl; } /** * @param string url of the RPC server */ public function setServerUrl($value) { $this->_serverUrl = $value; } }