Response.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Amazon
  17. * @subpackage SimpleDb
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Http_Response
  23. */
  24. require_once 'Zend/Http/Response.php';
  25. /** @see Zend_Xml_Security */
  26. require_once 'Zend/Xml/Security.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Service_Amazon
  30. * @subpackage SimpleDb
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Service_Amazon_SimpleDb_Response
  35. {
  36. /**
  37. * XML namespace used for SimpleDB responses.
  38. */
  39. protected $_xmlNamespace = 'http://sdb.amazonaws.com/doc/2009-04-15/';
  40. /**
  41. * The original HTTP response
  42. *
  43. * This contains the response body and headers.
  44. *
  45. * @var Zend_Http_Response
  46. */
  47. private $_httpResponse = null;
  48. /**
  49. * The response document object
  50. *
  51. * @var DOMDocument
  52. */
  53. private $_document = null;
  54. /**
  55. * The response XPath
  56. *
  57. * @var DOMXPath
  58. */
  59. private $_xpath = null;
  60. /**
  61. * Last error code
  62. *
  63. * @var integer
  64. */
  65. private $_errorCode = 0;
  66. /**
  67. * Last error message
  68. *
  69. * @var string
  70. */
  71. private $_errorMessage = '';
  72. /**
  73. * Creates a new high-level SimpleDB response object
  74. *
  75. * @param Zend_Http_Response $httpResponse the HTTP response.
  76. * @return void
  77. */
  78. public function __construct(Zend_Http_Response $httpResponse)
  79. {
  80. $this->_httpResponse = $httpResponse;
  81. }
  82. /**
  83. * Gets the XPath object for this response
  84. *
  85. * @return DOMXPath the XPath object for response.
  86. */
  87. public function getXPath()
  88. {
  89. if ($this->_xpath === null) {
  90. $document = $this->getDocument();
  91. if ($document === false) {
  92. $this->_xpath = false;
  93. } else {
  94. $this->_xpath = new DOMXPath($document);
  95. $this->_xpath->registerNamespace('sdb',
  96. $this->getNamespace());
  97. }
  98. }
  99. return $this->_xpath;
  100. }
  101. /**
  102. * Gets the SimpleXML document object for this response
  103. *
  104. * @return SimpleXMLElement
  105. */
  106. public function getSimpleXMLDocument()
  107. {
  108. try {
  109. $body = $this->_httpResponse->getBody();
  110. } catch (Zend_Http_Exception $e) {
  111. $body = false;
  112. }
  113. return Zend_Xml_Security::scan($body);
  114. }
  115. /**
  116. * Get HTTP response object
  117. *
  118. * @return Zend_Http_Response
  119. */
  120. public function getHttpResponse()
  121. {
  122. return $this->_httpResponse;
  123. }
  124. /**
  125. * Gets the document object for this response
  126. *
  127. * @return DOMDocument the DOM Document for this response.
  128. */
  129. public function getDocument()
  130. {
  131. try {
  132. $body = $this->_httpResponse->getBody();
  133. } catch (Zend_Http_Exception $e) {
  134. $body = false;
  135. }
  136. if ($this->_document === null) {
  137. if ($body !== false) {
  138. // turn off libxml error handling
  139. $errors = libxml_use_internal_errors();
  140. $this->_document = new DOMDocument();
  141. $this->_document = Zend_Xml_Security::scan($body, $this->_document);
  142. // reset libxml error handling
  143. libxml_clear_errors();
  144. libxml_use_internal_errors($errors);
  145. } else {
  146. $this->_document = false;
  147. }
  148. }
  149. return $this->_document;
  150. }
  151. /**
  152. * Return the current set XML Namespace.
  153. *
  154. * @return string
  155. */
  156. public function getNamespace()
  157. {
  158. return $this->_xmlNamespace;
  159. }
  160. /**
  161. * Set a new XML Namespace
  162. *
  163. * @param string $namespace
  164. */
  165. public function setNamespace($namespace)
  166. {
  167. $this->_xmlNamespace = $namespace;
  168. }
  169. }