Response.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-2015 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. /** @see Zend_Xml_Security */
  27. require_once 'Zend/Xml/Security.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Service_Amazon
  31. * @subpackage Ec2
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_Amazon_Ec2_Response {
  36. /**
  37. * XML namespace used for EC2 responses.
  38. */
  39. protected $_xmlNamespace = 'http://ec2.amazonaws.com/doc/2009-04-04/';
  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 EC2 response object
  74. *
  75. * @param Zend_Http_Response $httpResponse the HTTP response.
  76. */
  77. public function __construct(Zend_Http_Response $httpResponse)
  78. {
  79. $this->_httpResponse = $httpResponse;
  80. }
  81. /**
  82. * Gets the XPath object for this response
  83. *
  84. * @return DOMXPath the XPath object for response.
  85. */
  86. public function getXPath()
  87. {
  88. if ($this->_xpath === null) {
  89. $document = $this->getDocument();
  90. if ($document === false) {
  91. $this->_xpath = false;
  92. } else {
  93. $this->_xpath = new DOMXPath($document);
  94. $this->_xpath->registerNamespace('ec2',
  95. $this->getNamespace());
  96. }
  97. }
  98. return $this->_xpath;
  99. }
  100. /**
  101. * Gets the document object for this response
  102. *
  103. * @return DOMDocument the DOM Document for this response.
  104. */
  105. public function getDocument()
  106. {
  107. try {
  108. $body = $this->_httpResponse->getBody();
  109. } catch (Zend_Http_Exception $e) {
  110. $body = false;
  111. }
  112. if ($this->_document === null) {
  113. if ($body !== false) {
  114. // turn off libxml error handling
  115. $errors = libxml_use_internal_errors();
  116. $this->_document = new DOMDocument();
  117. $this->_document = Zend_Xml_Security::scan($body, $this->_document);
  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. }