AbstractRequest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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\Exception\InvalidUrlException;
  10. use JonnyW\PhantomJs\Exception\InvalidMethodException;
  11. use JonnyW\PhantomJs\Procedure\InputInterface;
  12. /**
  13. * PHP PhantomJs
  14. *
  15. * @author Jon Wenmoth <contact@jonnyw.me>
  16. */
  17. abstract class AbstractRequest
  18. implements RequestInterface, InputInterface
  19. {
  20. /**
  21. * Headers
  22. *
  23. * @var array
  24. * @access protected
  25. */
  26. protected $headers;
  27. /**
  28. * Request data
  29. *
  30. * @var array
  31. * @access protected
  32. */
  33. protected $data;
  34. /**
  35. * Request URL
  36. *
  37. * @var string
  38. * @access protected
  39. */
  40. protected $url;
  41. /**
  42. * Request method
  43. *
  44. * @var string
  45. * @access protected
  46. */
  47. protected $method;
  48. /**
  49. * Timeout period
  50. *
  51. * @var int
  52. * @access protected
  53. */
  54. protected $timeout;
  55. /**
  56. * Page load delay time.
  57. *
  58. * @var int
  59. * @access protected
  60. */
  61. protected $delay;
  62. /**
  63. * Viewport width.
  64. *
  65. * @var int
  66. * @access protected
  67. */
  68. protected $viewportWidth;
  69. /**
  70. * Viewport height.
  71. *
  72. * @var int
  73. * @access protected
  74. */
  75. protected $viewportHeight;
  76. /**
  77. * Internal constructor
  78. *
  79. * @access public
  80. * @param string $url (default: null)
  81. * @param string $method (default: RequestInterface::METHOD_GET)
  82. * @param int $timeout (default: 5000)
  83. */
  84. public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
  85. {
  86. $this->headers = array();
  87. $this->data = array();
  88. $this->delay = 0;
  89. $this->viewportWidth = 0;
  90. $this->viewportHeight = 0;
  91. $this->setMethod($method);
  92. $this->setTimeout($timeout);
  93. if ($url) {
  94. $this->setUrl($url);
  95. }
  96. }
  97. /**
  98. * Set request method
  99. *
  100. * @access public
  101. * @param string $method
  102. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  103. * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException
  104. */
  105. public function setMethod($method)
  106. {
  107. $method = strtoupper($method);
  108. $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface');
  109. if (!$reflection->hasConstant('METHOD_' . $method)) {
  110. throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
  111. }
  112. $this->method = $method;
  113. return $this;
  114. }
  115. /**
  116. * Get request method
  117. *
  118. * @access public
  119. * @return string
  120. */
  121. public function getMethod()
  122. {
  123. return $this->method;
  124. }
  125. /**
  126. * Set timeout period
  127. *
  128. * @access public
  129. * @param int $timeout
  130. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  131. */
  132. public function setTimeout($timeout)
  133. {
  134. $this->timeout = $timeout;
  135. return $this;
  136. }
  137. /**
  138. * Get timeout period
  139. *
  140. * @access public
  141. * @return int
  142. */
  143. public function getTimeout()
  144. {
  145. return $this->timeout;
  146. }
  147. /**
  148. * Set page load delay time (seconds).
  149. *
  150. * @access public
  151. * @param int $delay
  152. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  153. */
  154. public function setDelay($delay)
  155. {
  156. $this->delay = (int) $delay;
  157. return $this;
  158. }
  159. /**
  160. * Get page load delay time (seconds).
  161. *
  162. * @access public
  163. * @return int
  164. */
  165. public function getDelay()
  166. {
  167. return (int) $this->delay;
  168. }
  169. /**
  170. * Set viewport size.
  171. *
  172. * @access public
  173. * @param int $width
  174. * @param int $height
  175. * @return void
  176. */
  177. public function setViewportSize($width, $height)
  178. {
  179. $this->viewportWidth = (int) $width;
  180. $this->viewportHeight = (int) $height;
  181. }
  182. /**
  183. * Get viewport width.
  184. *
  185. * @access public
  186. * @return int
  187. */
  188. public function getViewportWidth()
  189. {
  190. return (int) $this->viewportWidth;
  191. }
  192. /**
  193. * Get viewport height.
  194. *
  195. * @access public
  196. * @return int
  197. */
  198. public function getViewportHeight()
  199. {
  200. return (int) $this->viewportHeight;
  201. }
  202. /**
  203. * Set request URL
  204. *
  205. * @access public
  206. * @param string $url
  207. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  208. * @throws \JonnyW\PhantomJs\Exception\InvalidUrlException
  209. */
  210. public function setUrl($url)
  211. {
  212. if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
  213. throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
  214. }
  215. $this->url = $url;
  216. return $this;
  217. }
  218. /**
  219. * Get request URL
  220. * - Assembles query string for GET
  221. * and HEAD requests
  222. *
  223. * @access public
  224. * @return string
  225. */
  226. public function getUrl()
  227. {
  228. if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
  229. return $this->url;
  230. }
  231. $url = $this->url;
  232. if (count($this->data)) {
  233. $url .= false === strpos($url, '?') ? '?' : '&';
  234. $url .= http_build_query($this->data);
  235. }
  236. return $url;
  237. }
  238. /**
  239. * Get content body
  240. * - Returns query string if not GET or HEAD
  241. *
  242. * @access public
  243. * @return string
  244. */
  245. public function getBody()
  246. {
  247. if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
  248. return '';
  249. }
  250. return http_build_query($this->getRequestData());
  251. }
  252. /**
  253. * Set request data
  254. *
  255. * @access public
  256. * @param array $data
  257. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  258. */
  259. public function setRequestData(array $data)
  260. {
  261. $this->data = $data;
  262. return $this;
  263. }
  264. /**
  265. * Get request data
  266. *
  267. * @access public
  268. * @param boolean $flat
  269. * @return array
  270. */
  271. public function getRequestData($flat = true)
  272. {
  273. if ($flat) {
  274. return $this->flattenData($this->data);
  275. }
  276. return $this->data;
  277. }
  278. /**
  279. * Set headers
  280. *
  281. * @access public
  282. * @param array $headers
  283. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  284. */
  285. public function setHeaders(array $headers)
  286. {
  287. $this->headers = $headers;
  288. }
  289. /**
  290. * Add single header
  291. *
  292. * @access public
  293. * @param string $header
  294. * @param string $value
  295. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  296. */
  297. public function addHeader($header, $value)
  298. {
  299. $this->headers[$header] = $value;
  300. return $this;
  301. }
  302. /**
  303. * Merge headers with existing
  304. *
  305. * @access public
  306. * @param array $headers
  307. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  308. */
  309. public function addHeaders(array $headers)
  310. {
  311. $this->headers = array_merge($this->headers, $headers);
  312. return $this;
  313. }
  314. /**
  315. * Get request headers
  316. *
  317. * @access public
  318. * @param string $format
  319. * @return array
  320. */
  321. public function getHeaders($format = 'default')
  322. {
  323. if ($format === 'json') {
  324. return json_encode($this->headers);
  325. }
  326. return $this->headers;
  327. }
  328. /**
  329. * Flatten data into single
  330. * dimensional array
  331. *
  332. * @access protected
  333. * @param array $data
  334. * @param string $prefix
  335. * @param string $format
  336. * @return array
  337. */
  338. protected function flattenData(array $data, $prefix = '', $format = '%s')
  339. {
  340. $flat = array();
  341. foreach ($data as $name => $value) {
  342. $ref = $prefix . sprintf($format, $name);
  343. if (is_array($value)) {
  344. $flat += $this->flattenData($value, $ref, '[%s]');
  345. continue;
  346. }
  347. $flat[$ref] = $value;
  348. }
  349. return $flat;
  350. }
  351. }