Response.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Http;
  9. use JonnyW\PhantomJs\Procedure\OutputInterface;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class Response
  16. implements ResponseInterface, OutputInterface
  17. {
  18. /**
  19. * Http headers array
  20. *
  21. * @var array
  22. * @access public
  23. */
  24. public $headers;
  25. /**
  26. * Response int
  27. *
  28. * @var string
  29. * @access public
  30. */
  31. public $status;
  32. /**
  33. * Response body
  34. *
  35. * @var string
  36. * @access public
  37. */
  38. public $content;
  39. /**
  40. * Response content type header
  41. *
  42. * @var string
  43. * @access public
  44. */
  45. public $contentType;
  46. /**
  47. * Requested URL
  48. *
  49. * @var string
  50. * @access public
  51. */
  52. public $url;
  53. /**
  54. * Redirected URL
  55. *
  56. * @var string
  57. * @access public
  58. */
  59. public $redirectURL;
  60. /**
  61. * Request time string
  62. *
  63. * @var string
  64. * @access public
  65. */
  66. public $time;
  67. /**
  68. * Console messages
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. public $console;
  74. /**
  75. * Session cookies
  76. *
  77. * @var array
  78. * @access public
  79. */
  80. public $cookies;
  81. /**
  82. * Import response data
  83. *
  84. * @access public
  85. * @param array $data
  86. * @return \JonnyW\PhantomJs\Http\Response
  87. */
  88. public function import(array $data)
  89. {
  90. foreach ($data as $param => $value) {
  91. if ($param === 'headers') {
  92. continue;
  93. }
  94. if (property_exists($this, $param)) {
  95. $this->$param = $value;
  96. }
  97. }
  98. $this->headers = array();
  99. if (isset($data['headers'])) {
  100. $this->setHeaders((array) $data['headers']);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Set headers array
  106. *
  107. * @access protected
  108. * @param array $headers
  109. * @return \JonnyW\PhantomJs\Http\Response
  110. */
  111. protected function setHeaders(array $headers)
  112. {
  113. foreach ($headers as $header) {
  114. if (isset($header['name']) && isset($header['value'])) {
  115. $this->headers[$header['name']] = $header['value'];
  116. }
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Get HTTP headers array
  122. *
  123. * @access public
  124. * @return array
  125. */
  126. public function getHeaders()
  127. {
  128. return (array) $this->headers;
  129. }
  130. /**
  131. * Get HTTP header value for code
  132. *
  133. * @access public
  134. * @param string $code
  135. * @return mixed
  136. */
  137. public function getHeader($code)
  138. {
  139. if (isset($this->headers[$code])) {
  140. return $this->headers[$code];
  141. }
  142. return null;
  143. }
  144. /**
  145. * Get response status code
  146. *
  147. * @access public
  148. * @return integer
  149. */
  150. public function getStatus()
  151. {
  152. return (int) $this->status;
  153. }
  154. /**
  155. * Get page content from response
  156. *
  157. * @access public
  158. * @return string
  159. */
  160. public function getContent()
  161. {
  162. return $this->content;
  163. }
  164. /**
  165. * Get content type header
  166. *
  167. * @access public
  168. * @return string
  169. */
  170. public function getContentType()
  171. {
  172. return $this->contentType;
  173. }
  174. /**
  175. * Get request URL
  176. *
  177. * @access public
  178. * @return string
  179. */
  180. public function getUrl()
  181. {
  182. return $this->url;
  183. }
  184. /**
  185. * Get redirect URL (if redirected)
  186. *
  187. * @access public
  188. * @return string
  189. */
  190. public function getRedirectUrl()
  191. {
  192. return $this->redirectURL;
  193. }
  194. /**
  195. * Is response a redirect
  196. * - Checks status codes
  197. *
  198. * @access public
  199. * @return boolean
  200. */
  201. public function isRedirect()
  202. {
  203. $status = $this->getStatus();
  204. return (bool) ($status >= 300 && $status <= 307);
  205. }
  206. /**
  207. * Get time string
  208. *
  209. * @access public
  210. * @return string
  211. */
  212. public function getTime()
  213. {
  214. return $this->time;
  215. }
  216. /**
  217. * Get console messages
  218. *
  219. * @access public
  220. * @return array
  221. */
  222. public function getConsole()
  223. {
  224. return $this->console;
  225. }
  226. /**
  227. * Get session cookies
  228. *
  229. * @access public
  230. * @return array
  231. */
  232. public function getCookies()
  233. {
  234. return $this->cookies;
  235. }
  236. }