| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace JonnyW\PhantomJs\Http;
- use JonnyW\PhantomJs\Procedure\OutputInterface;
- /**
- * PHP PhantomJs
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
- class Response
- implements ResponseInterface, OutputInterface
- {
- /**
- * Http headers array
- *
- * @var array
- * @access public
- */
- public $headers;
- /**
- * Response int
- *
- * @var string
- * @access public
- */
- public $status;
- /**
- * Response body
- *
- * @var string
- * @access public
- */
- public $content;
- /**
- * Response content type header
- *
- * @var string
- * @access public
- */
- public $contentType;
- /**
- * Requested URL
- *
- * @var string
- * @access public
- */
- public $url;
- /**
- * Redirected URL
- *
- * @var string
- * @access public
- */
- public $redirectURL;
- /**
- * Request time string
- *
- * @var string
- * @access public
- */
- public $time;
- /**
- * Console messages
- *
- * @var array
- * @access public
- */
- public $console;
- /**
- * Session cookies
- *
- * @var array
- * @access public
- */
- public $cookies;
- /**
- * Import response data
- *
- * @access public
- * @param array $data
- * @return \JonnyW\PhantomJs\Http\Response
- */
- public function import(array $data)
- {
- foreach ($data as $param => $value) {
- if ($param === 'headers') {
- continue;
- }
- if (property_exists($this, $param)) {
- $this->$param = $value;
- }
- }
- $this->headers = array();
- if (isset($data['headers'])) {
- $this->setHeaders((array) $data['headers']);
- }
- return $this;
- }
- /**
- * Set headers array
- *
- * @access protected
- * @param array $headers
- * @return \JonnyW\PhantomJs\Http\Response
- */
- protected function setHeaders(array $headers)
- {
- foreach ($headers as $header) {
- if (isset($header['name']) && isset($header['value'])) {
- $this->headers[$header['name']] = $header['value'];
- }
- }
- return $this;
- }
- /**
- * Get HTTP headers array
- *
- * @access public
- * @return array
- */
- public function getHeaders()
- {
- return (array) $this->headers;
- }
- /**
- * Get HTTP header value for code
- *
- * @access public
- * @param string $code
- * @return mixed
- */
- public function getHeader($code)
- {
- if (isset($this->headers[$code])) {
- return $this->headers[$code];
- }
- return null;
- }
- /**
- * Get response status code
- *
- * @access public
- * @return integer
- */
- public function getStatus()
- {
- return (int) $this->status;
- }
- /**
- * Get page content from response
- *
- * @access public
- * @return string
- */
- public function getContent()
- {
- return $this->content;
- }
- /**
- * Get content type header
- *
- * @access public
- * @return string
- */
- public function getContentType()
- {
- return $this->contentType;
- }
- /**
- * Get request URL
- *
- * @access public
- * @return string
- */
- public function getUrl()
- {
- return $this->url;
- }
- /**
- * Get redirect URL (if redirected)
- *
- * @access public
- * @return string
- */
- public function getRedirectUrl()
- {
- return $this->redirectURL;
- }
- /**
- * Is response a redirect
- * - Checks status codes
- *
- * @access public
- * @return boolean
- */
- public function isRedirect()
- {
- $status = $this->getStatus();
- return (bool) ($status >= 300 && $status <= 307);
- }
- /**
- * Get time string
- *
- * @access public
- * @return string
- */
- public function getTime()
- {
- return $this->time;
- }
- /**
- * Get console messages
- *
- * @access public
- * @return array
- */
- public function getConsole()
- {
- return $this->console;
- }
- /**
- * Get session cookies
- *
- * @access public
- * @return array
- */
- public function getCookies()
- {
- return $this->cookies;
- }
- }
|