Response.php 3.5 KB

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