Response.php 4.4 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-2012 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. /**
  26. * @category Zend
  27. * @package Zend_Service_Amazon
  28. * @subpackage SimpleDb
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Service_Amazon_SimpleDb_Response
  33. {
  34. /**
  35. * XML namespace used for SimpleDB responses.
  36. */
  37. protected $_xmlNamespace = 'http://sdb.amazonaws.com/doc/2009-04-15/';
  38. /**
  39. * The original HTTP response
  40. *
  41. * This contains the response body and headers.
  42. *
  43. * @var Zend_Http_Response
  44. */
  45. private $_httpResponse = null;
  46. /**
  47. * The response document object
  48. *
  49. * @var DOMDocument
  50. */
  51. private $_document = null;
  52. /**
  53. * The response XPath
  54. *
  55. * @var DOMXPath
  56. */
  57. private $_xpath = null;
  58. /**
  59. * Last error code
  60. *
  61. * @var integer
  62. */
  63. private $_errorCode = 0;
  64. /**
  65. * Last error message
  66. *
  67. * @var string
  68. */
  69. private $_errorMessage = '';
  70. /**
  71. * Creates a new high-level SimpleDB response object
  72. *
  73. * @param Zend_Http_Response $httpResponse the HTTP response.
  74. * @return void
  75. */
  76. public function __construct(Zend_Http_Response $httpResponse)
  77. {
  78. $this->_httpResponse = $httpResponse;
  79. }
  80. /**
  81. * Gets the XPath object for this response
  82. *
  83. * @return DOMXPath the XPath object for response.
  84. */
  85. public function getXPath()
  86. {
  87. if ($this->_xpath === null) {
  88. $document = $this->getDocument();
  89. if ($document === false) {
  90. $this->_xpath = false;
  91. } else {
  92. $this->_xpath = new DOMXPath($document);
  93. $this->_xpath->registerNamespace('sdb',
  94. $this->getNamespace());
  95. }
  96. }
  97. return $this->_xpath;
  98. }
  99. /**
  100. * Gets the SimpleXML document object for this response
  101. *
  102. * @return SimpleXMLElement
  103. */
  104. public function getSimpleXMLDocument()
  105. {
  106. try {
  107. $body = $this->_httpResponse->getBody();
  108. } catch (Zend_Http_Exception $e) {
  109. $body = false;
  110. }
  111. return simplexml_load_string($body);
  112. }
  113. /**
  114. * Get HTTP response object
  115. *
  116. * @return Zend_Http_Response
  117. */
  118. public function getHttpResponse()
  119. {
  120. return $this->_httpResponse;
  121. }
  122. /**
  123. * Gets the document object for this response
  124. *
  125. * @return DOMDocument the DOM Document for this response.
  126. */
  127. public function getDocument()
  128. {
  129. try {
  130. $body = $this->_httpResponse->getBody();
  131. } catch (Zend_Http_Exception $e) {
  132. $body = false;
  133. }
  134. if ($this->_document === null) {
  135. if ($body !== false) {
  136. // turn off libxml error handling
  137. $errors = libxml_use_internal_errors();
  138. $this->_document = new DOMDocument();
  139. if (!$this->_document->loadXML($body)) {
  140. $this->_document = false;
  141. }
  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. }