Response.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Ec2
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Http_Response
  24. */
  25. require_once 'Zend/Http/Response.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_Amazon
  29. * @subpackage Ec2
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_Amazon_Ec2_Response {
  34. /**
  35. * XML namespace used for EC2 responses.
  36. */
  37. protected $_xmlNamespace = 'http://ec2.amazonaws.com/doc/2009-04-04/';
  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 EC2 response object
  72. *
  73. * @param Zend_Http_Response $httpResponse the HTTP response.
  74. */
  75. public function __construct(Zend_Http_Response $httpResponse)
  76. {
  77. $this->_httpResponse = $httpResponse;
  78. }
  79. /**
  80. * Gets the XPath object for this response
  81. *
  82. * @return DOMXPath the XPath object for response.
  83. */
  84. public function getXPath()
  85. {
  86. if ($this->_xpath === null) {
  87. $document = $this->getDocument();
  88. if ($document === false) {
  89. $this->_xpath = false;
  90. } else {
  91. $this->_xpath = new DOMXPath($document);
  92. $this->_xpath->registerNamespace('ec2',
  93. $this->getNamespace());
  94. }
  95. }
  96. return $this->_xpath;
  97. }
  98. /**
  99. * Gets the document object for this response
  100. *
  101. * @return DOMDocument the DOM Document for this response.
  102. */
  103. public function getDocument()
  104. {
  105. try {
  106. $body = $this->_httpResponse->getBody();
  107. } catch (Zend_Http_Exception $e) {
  108. $body = false;
  109. }
  110. if ($this->_document === null) {
  111. if ($body !== false) {
  112. // turn off libxml error handling
  113. $errors = libxml_use_internal_errors();
  114. $this->_document = new DOMDocument();
  115. if (!$this->_document->loadXML($body)) {
  116. $this->_document = false;
  117. }
  118. // reset libxml error handling
  119. libxml_clear_errors();
  120. libxml_use_internal_errors($errors);
  121. } else {
  122. $this->_document = false;
  123. }
  124. }
  125. return $this->_document;
  126. }
  127. /**
  128. * Return the current set XML Namespace.
  129. *
  130. * @return string
  131. */
  132. public function getNamespace()
  133. {
  134. return $this->_xmlNamespace;
  135. }
  136. /**
  137. * Set a new XML Namespace
  138. *
  139. * @param string $namespace
  140. */
  141. public function setNamespace($namespace)
  142. {
  143. $this->_xmlNamespace = $namespace;
  144. }
  145. }