7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Controller
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
24 require_once 'Zend/XmlRpc/Value.php';
29 require_once 'Zend/XmlRpc/Fault.php';
32 * XmlRpc Request object
34 * Encapsulates an XmlRpc request, holding the method call and all parameters.
35 * Provides accessors for these, as well as the ability to load from XML and to
36 * create the XML request string.
38 * Additionally, if errors occur setting the method or parsing XML, a fault is
39 * generated and stored in {@link $_fault}; developers may check for it using
40 * {@link isFault()} and {@link getFault()}.
43 * @package Zend_XmlRpc
44 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 * @version $Id: Request.php 17786 2009-08-23 22:26:33Z lars $
48 class Zend_XmlRpc_Request
51 * Request character encoding
54 protected $_encoding = 'UTF-8';
72 protected $_params = array();
75 * Fault object, if any
76 * @var Zend_XmlRpc_Fault
78 protected $_fault = null;
81 * XML-RPC type for each param
84 protected $_types = array();
87 * XML-RPC request params
90 protected $_xmlRpcParams = array();
93 * Create a new XML-RPC request
95 * @param string $method (optional)
96 * @param array $params (optional)
98 public function __construct($method = null, $params = null)
100 if ($method !== null) {
101 $this->setMethod($method);
104 if ($params !== null) {
105 $this->setParams($params);
111 * Set encoding to use in request
113 * @param string $encoding
114 * @return Zend_XmlRpc_Request
116 public function setEncoding($encoding)
118 $this->_encoding
= $encoding;
123 * Retrieve current request encoding
127 public function getEncoding()
129 return $this->_encoding
;
135 * @param string $method
136 * @return boolean Returns true on success, false if method name is invalid
138 public function setMethod($method)
140 if (!is_string($method) ||
!preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
141 $this->_fault
= new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")');
142 $this->_fault
->setEncoding($this->getEncoding());
146 $this->_method
= $method;
151 * Retrieve call method
155 public function getMethod()
157 return $this->_method
;
161 * Add a parameter to the parameter stack
163 * Adds a parameter to the parameter stack, associating it with the type
166 * @param mixed $value
167 * @param string $type Optional; type hinting
170 public function addParam($value, $type = null)
172 $this->_params
[] = $value;
173 if (null === $type) {
174 // Detect type if not provided explicitly
175 if ($value instanceof Zend_XmlRpc_Value
) {
176 $type = $value->getType();
178 $xmlRpcValue = Zend_XmlRpc_Value
::getXmlRpcValue($value);
179 $type = $xmlRpcValue->getType();
182 $this->_types
[] = $type;
183 $this->_xmlRpcParams
[] = array('value' => $value, 'type' => $type);
187 * Set the parameters array
189 * If called with a single, array value, that array is used to set the
190 * parameters stack. If called with multiple values or a single non-array
191 * value, the arguments are used to set the parameters stack.
193 * Best is to call with array of the format, in order to allow type hinting
194 * when creating the XMLRPC values for each parameter:
207 public function setParams()
209 $argc = func_num_args();
210 $argv = func_get_args();
215 if ((1 == $argc) && is_array($argv[0])) {
219 foreach ($argv[0] as $arg) {
220 if (!is_array($arg) ||
!isset($arg['value'])) {
224 $params[] = $arg['value'];
226 if (!isset($arg['type'])) {
227 $xmlRpcValue = Zend_XmlRpc_Value
::getXmlRpcValue($arg['value']);
228 $arg['type'] = $xmlRpcValue->getType();
230 $types[] = $arg['type'];
233 $this->_xmlRpcParams
= $argv[0];
234 $this->_params
= $params;
235 $this->_types
= $types;
237 $this->_params
= $argv[0];
238 $this->_types
= array();
239 $xmlRpcParams = array();
240 foreach ($argv[0] as $arg) {
241 if ($arg instanceof Zend_XmlRpc_Value
) {
242 $type = $arg->getType();
244 $xmlRpcValue = Zend_XmlRpc_Value
::getXmlRpcValue($arg);
245 $type = $xmlRpcValue->getType();
247 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
248 $this->_types
[] = $type;
250 $this->_xmlRpcParams
= $xmlRpcParams;
255 $this->_params
= $argv;
256 $this->_types
= array();
257 $xmlRpcParams = array();
258 foreach ($argv as $arg) {
259 if ($arg instanceof Zend_XmlRpc_Value
) {
260 $type = $arg->getType();
262 $xmlRpcValue = Zend_XmlRpc_Value
::getXmlRpcValue($arg);
263 $type = $xmlRpcValue->getType();
265 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
266 $this->_types
[] = $type;
268 $this->_xmlRpcParams
= $xmlRpcParams;
272 * Retrieve the array of parameters
276 public function getParams()
278 return $this->_params
;
282 * Return parameter types
286 public function getTypes()
288 return $this->_types
;
292 * Load XML and parse into request components
294 * @param string $request
295 * @return boolean True on success, false if an error occurred.
297 public function loadXml($request)
299 if (!is_string($request)) {
300 $this->_fault
= new Zend_XmlRpc_Fault(635);
301 $this->_fault
->setEncoding($this->getEncoding());
306 $xml = @new
SimpleXMLElement($request);
307 } catch (Exception
$e) {
309 $this->_fault
= new Zend_XmlRpc_Fault(631);
310 $this->_fault
->setEncoding($this->getEncoding());
314 // Check for method name
315 if (empty($xml->methodName
)) {
316 // Missing method name
317 $this->_fault
= new Zend_XmlRpc_Fault(632);
318 $this->_fault
->setEncoding($this->getEncoding());
322 $this->_method
= (string) $xml->methodName
;
324 // Check for parameters
325 if (!empty($xml->params
)) {
328 foreach ($xml->params
->children() as $param) {
329 if (!isset($param->value
)) {
330 $this->_fault
= new Zend_XmlRpc_Fault(633);
331 $this->_fault
->setEncoding($this->getEncoding());
336 $param = Zend_XmlRpc_Value
::getXmlRpcValue($param->value
, Zend_XmlRpc_Value
::XML_STRING
);
337 $types[] = $param->getType();
338 $argv[] = $param->getValue();
339 } catch (Exception
$e) {
340 $this->_fault
= new Zend_XmlRpc_Fault(636);
341 $this->_fault
->setEncoding($this->getEncoding());
346 $this->_types
= $types;
347 $this->_params
= $argv;
350 $this->_xml
= $request;
356 * Does the current request contain errors and should it return a fault
361 public function isFault()
363 return $this->_fault
instanceof Zend_XmlRpc_Fault
;
367 * Retrieve the fault response, if any
369 * @return null|Zend_XmlRpc_Fault
371 public function getFault()
373 return $this->_fault
;
377 * Retrieve method parameters as XMLRPC values
381 protected function _getXmlRpcParams()
384 if (is_array($this->_xmlRpcParams
)) {
385 foreach ($this->_xmlRpcParams
as $param) {
386 $value = $param['value'];
387 $type = isset($param['type']) ?
$param['type'] : Zend_XmlRpc_Value
::AUTO_DETECT_TYPE
;
389 if (!$value instanceof Zend_XmlRpc_Value
) {
390 $value = Zend_XmlRpc_Value
::getXmlRpcValue($value, $type);
404 public function saveXML()
406 $args = $this->_getXmlRpcParams();
407 $method = $this->getMethod();
409 $dom = new DOMDocument('1.0', $this->getEncoding());
410 $mCall = $dom->appendChild($dom->createElement('methodCall'));
411 $mName = $mCall->appendChild($dom->createElement('methodName', $method));
413 if (is_array($args) && count($args)) {
414 $params = $mCall->appendChild($dom->createElement('params'));
416 foreach ($args as $arg) {
417 /* @var $arg Zend_XmlRpc_Value */
418 $argDOM = new DOMDocument('1.0', $this->getEncoding());
419 $argDOM->loadXML($arg->saveXML());
421 $param = $params->appendChild($dom->createElement('param'));
422 $param->appendChild($dom->importNode($argDOM->documentElement
, 1));
426 return $dom->saveXML();
434 public function __toString()
436 return $this->saveXML();