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.
12 * @property array $hdrs deprecated, use $httpResponse['headers']
13 * @property array _cookies deprecated, use $httpResponse['cookies']
14 * @property string $raw_data deprecated, use $httpResponse['raw_data']
18 protected static $charsetEncoder;
20 /// @todo: do these need to be public?
30 public $content_type = 'text/xml';
31 protected $httpResponse = array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null);
33 public function getCharsetEncoder()
35 if (self::$charsetEncoder === null) {
36 self::$charsetEncoder = Charset::instance();
38 return self::$charsetEncoder;
41 public function setCharsetEncoder($charsetEncoder)
43 self::$charsetEncoder = $charsetEncoder;
47 * @param Value|string|mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string)
48 * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
49 * @param string $fString the error string, in case of an error response
50 * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
51 * the code guess the correct type.
52 * @param array|null $httpResponse
54 * @todo add check that $val / $fCode / $fString is of correct type???
55 * 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
56 * xml chunk, depending on usage of Client::send() inside which creator is called...
58 public function __construct($val, $fCode = 0, $fString = '', $valType = '', $httpResponse = null)
62 $this->errno = $fCode;
63 $this->errstr = $fString;
65 // successful response
68 // user did not declare type of response value: try to guess it
69 if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
70 $this->valtyp = 'xmlrpcvals';
71 } elseif (is_string($this->val)) {
72 $this->valtyp = 'xml';
74 $this->valtyp = 'phpvals';
77 // user declares type of resp value: believe him
78 $this->valtyp = $valType;
82 if (is_array($httpResponse)) {
83 $this->httpResponse = array_merge(array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null), $httpResponse);
88 * Returns the error code of the response.
90 * @return integer the error code of this response (0 for not-error responses)
92 public function faultCode()
98 * Returns the error code of the response.
100 * @return string the error string of this response ('' for not-error responses)
102 public function faultString()
104 return $this->errstr;
108 * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
109 * method should not be used (it may not even be an object).
111 * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
112 * depending on the convention adopted when creating the Response
114 public function value()
120 * Returns an array with the cookies received from the server.
121 * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
122 * with attributes being e.g. 'expires', 'path', domain'.
123 * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
124 * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
125 * with the next request to the server (using Client::setCookie) or not.
127 * @return array[] array of cookies received from the server
129 public function cookies()
131 return $this->httpResponse['cookies'];
135 * @return array array with keys 'headers', 'cookies', 'raw_data' and 'status_code'
137 public function httpResponse()
139 return $this->httpResponse;
143 * Returns xml representation of the response. XML prologue not included.
145 * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
147 * @return string the xml representation of the response
151 public function serialize($charsetEncoding = '')
153 if ($charsetEncoding != '') {
154 $this->content_type = 'text/xml; charset=' . $charsetEncoding;
156 $this->content_type = 'text/xml';
158 if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
159 $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
161 $result = "<methodResponse>\n";
164 // Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars
165 $result .= "<fault>\n" .
166 "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
167 "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
168 Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
169 "</struct>\n</value>\n</fault>";
171 if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
172 if (is_string($this->val) && $this->valtyp == 'xml') {
173 $result .= "<params>\n<param>\n" .
175 "</param>\n</params>";
177 /// @todo try to build something serializable using the Encoder...
178 throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
181 $result .= "<params>\n<param>\n" .
182 $this->val->serialize($charsetEncoding) .
183 "</param>\n</params>";
186 $result .= "\n</methodResponse>";
187 $this->payload = $result;
194 public function __get($name)
196 //trigger_error('getting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
200 return $this->httpResponse['headers'];
202 return $this->httpResponse['cookies'];
204 return $this->httpResponse['raw_data'];
206 $trace = debug_backtrace();
207 trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
212 public function __set($name, $value)
214 //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
218 $this->httpResponse['headers'] = $value;
221 $this->httpResponse['cookies'] = $value;
224 $this->httpResponse['raw_data'] = $value;
227 $trace = debug_backtrace();
228 trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
232 public function __isset($name)
236 return isset($this->httpResponse['headers']);
238 return isset($this->httpResponse['cookies']);
240 return isset($this->httpResponse['raw_data']);
246 public function __unset($name)
250 unset($this->httpResponse['headers']);
253 unset($this->httpResponse['cookies']);
256 unset($this->httpResponse['raw_data']);
259 $trace = debug_backtrace();
260 trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);