1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<?php
/**
* @author Robin J. Rogge <rrogge@bigpoint.net>
* @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:
* <pre>
* $_rpcClient = TRpcClient::create('xml', 'http://host/server');
* $_result = $_rpcClient->remoteMethodName($param, $otherParam);
* </pre>
*
* or as oneliner:
* <pre>
* $_result = TRpcClient::create('json', 'http://host/server')->remoteMethod($param, ...);
* </pre>
*
* Second, you can also use the specific implementation directly:
* <pre>
* $_rpcClient = new TXmlRpcClient('http://host/server');
* $_result = $_rpcClient->remoteMethod($param, ...);
* </pre>
*
* or as oneliner:
* <pre>
* $_result = TXmlRpcClient('http://host/server')->hello();
* </pre>
*
* @author Robin J. Rogge <rrogge@bigpoint.net>
* @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;
}
}
|