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 protected static $charsetEncoder;
16 /// @todo: do these need to be public?
26 public $content_type = 'text/xml';
27 public $hdrs = array();
28 public $_cookies = array();
29 public $raw_data = '';
31 public function getCharsetEncoder()
33 if (self::$charsetEncoder === null) {
34 self::$charsetEncoder = Charset::instance();
36 return self::$charsetEncoder;
39 public function setCharsetEncoder($charsetEncoder)
41 self::$charsetEncoder = $charsetEncoder;
45 * @param Value|string|mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string)
46 * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
47 * @param string $fString the error string, in case of an error response
48 * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
49 * the code guess the correct type.
51 * @todo add check that $val / $fCode / $fString is of correct type???
52 * 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
53 * xml chunk, depending on usage of Client::send() inside which creator is called...
55 public function __construct($val, $fCode = 0, $fString = '', $valType = '')
59 $this->errno = $fCode;
60 $this->errstr = $fString;
62 // successful response
65 // user did not declare type of response value: try to guess it
66 if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
67 $this->valtyp = 'xmlrpcvals';
68 } elseif (is_string($this->val)) {
69 $this->valtyp = 'xml';
71 $this->valtyp = 'phpvals';
74 // user declares type of resp value: believe him
75 $this->valtyp = $valType;
81 * Returns the error code of the response.
83 * @return integer the error code of this response (0 for not-error responses)
85 public function faultCode()
91 * Returns the error code of the response.
93 * @return string the error string of this response ('' for not-error responses)
95 public function faultString()
101 * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
102 * method should not be used (it may not even be an object).
104 * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
105 * depending on the convention adopted when creating the Response
107 public function value()
113 * Returns an array with the cookies received from the server.
114 * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
115 * with attributes being e.g. 'expires', 'path', domain'.
116 * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
117 * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
118 * with the next request to the server (using Client::setCookie) or not.
120 * @return array[] array of cookies received from the server
122 public function cookies()
124 return $this->_cookies;
128 * Returns xml representation of the response. XML prologue not included.
130 * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
132 * @return string the xml representation of the response
136 public function serialize($charsetEncoding = '')
138 if ($charsetEncoding != '') {
139 $this->content_type = 'text/xml; charset=' . $charsetEncoding;
141 $this->content_type = 'text/xml';
143 if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
144 $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
146 $result = "<methodResponse>\n";
149 // Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars
150 $result .= "<fault>\n" .
151 "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
152 "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
153 Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
154 "</struct>\n</value>\n</fault>";
156 if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
157 if (is_string($this->val) && $this->valtyp == 'xml') {
158 $result .= "<params>\n<param>\n" .
160 "</param>\n</params>";
162 /// @todo try to build something serializable using the Encoder...
163 throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
166 $result .= "<params>\n<param>\n" .
167 $this->val->serialize($charsetEncoding) .
168 "</param>\n</params>";
171 $result .= "\n</methodResponse>";
172 $this->payload = $result;