5 use PhpXmlRpc\Helper\Charset;
8 * This class provides the representation of the response of an XML-RPC server.
9 * Server-side, a server method handler will construct a Response and pass it as its return value.
10 * An identical Response object will be returned by the result of an invocation of the send() method of the Client class.
14 /// @todo: do these need to be public?
20 public $hdrs = array();
21 public $_cookies = array();
22 public $content_type = 'text/xml';
23 public $raw_data = '';
26 * @param mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string)
27 * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
28 * @param string $fString the error string, in case of an error response
29 * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
30 * the code guess the correct type.
32 * @todo add check that $val / $fCode / $fString is of correct type???
33 * NB: as of now we do not do it, since it might be either an xmlrpc value or a plain php val, or a complete
34 * xml chunk, depending on usage of Client::send() inside which creator is called...
36 public function __construct($val, $fCode = 0, $fString = '', $valType = '')
40 $this->errno = $fCode;
41 $this->errstr = $fString;
43 // successful response
46 // user did not declare type of response value: try to guess it
47 if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
48 $this->valtyp = 'xmlrpcvals';
49 } elseif (is_string($this->val)) {
50 $this->valtyp = 'xml';
52 $this->valtyp = 'phpvals';
55 // user declares type of resp value: believe him
56 $this->valtyp = $valType;
62 * Returns the error code of the response.
64 * @return integer the error code of this response (0 for not-error responses)
66 public function faultCode()
72 * Returns the error code of the response.
74 * @return string the error string of this response ('' for not-error responses)
76 public function faultString()
82 * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
83 * method should not be used (it may not even be an object).
85 * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
86 * depending on the convention adopted when creating the Response
88 public function value()
94 * Returns an array with the cookies received from the server.
95 * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
96 * with attributes being e.g. 'expires', 'path', domain'.
97 * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
98 * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
99 * with the next request to the server (using Client::setCookie) or not.
101 * @return array array of cookies received from the server
103 public function cookies()
105 return $this->_cookies;
109 * Returns xml representation of the response. XML prologue not included.
111 * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
113 * @return string the xml representation of the response
117 public function serialize($charsetEncoding = '')
119 if ($charsetEncoding != '') {
120 $this->content_type = 'text/xml; charset=' . $charsetEncoding;
122 $this->content_type = 'text/xml';
124 if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
125 $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
127 $result = "<methodResponse>\n";
130 // Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars
131 $result .= "<fault>\n" .
132 "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
133 "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
134 Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
135 "</struct>\n</value>\n</fault>";
137 if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
138 if (is_string($this->val) && $this->valtyp == 'xml') {
139 $result .= "<params>\n<param>\n" .
141 "</param>\n</params>";
143 /// @todo try to build something serializable?
144 throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
147 $result .= "<params>\n<param>\n" .
148 $this->val->serialize($charsetEncoding) .
149 "</param>\n</params>";
152 $result .= "\n</methodResponse>";
153 $this->payload = $result;