/sites/all/modules/dcodr/dcodr_core/lib/RestRequest.class.php

https://bitbucket.org/pixellight/pixellight-intranet · PHP · 254 lines · 207 code · 43 blank · 4 comment · 4 complexity · 8c836655adcd2ba800ca2f8a46b41f72 MD5 · raw file

  1. <?php
  2. /**
  3. * Project: dcodr
  4. * File: RestRequest.class.php
  5. */
  6. class RestRequest
  7. {
  8. protected $url;
  9. protected $verb;
  10. protected $requestBody;
  11. protected $requestLength;
  12. protected $username;
  13. protected $password;
  14. protected $contentType;
  15. protected $acceptType;
  16. protected $userAgent;
  17. protected $response;
  18. protected $raw;
  19. public function __construct ($url = null, $verb = 'GET', $requestBody = null)
  20. {
  21. $this->url = $url;
  22. $this->verb = $verb;
  23. $this->requestBody = $requestBody;
  24. $this->requestLength = 0;
  25. $this->contentType = 'application/json; charset=utf-8';
  26. $this->acceptType = 'application/json';
  27. $this->userAgent = "ShaneTestApp (shane@pixellight.com.au)";
  28. $this->raw = null;
  29. $this->response = array();
  30. }
  31. public function flush ()
  32. {
  33. $this->requestBody = null;
  34. $this->requestLength = 0;
  35. $this->verb = 'GET';
  36. $this->response = array();
  37. $this->raw = null;
  38. }
  39. public function execute()
  40. {
  41. $ch = curl_init();
  42. $this->setAuth($ch);
  43. try
  44. {
  45. switch (strtoupper($this->verb))
  46. {
  47. case 'GET':
  48. $this->executeGet($ch);
  49. break;
  50. case 'POST':
  51. $this->executePost($ch);
  52. break;
  53. case 'POSTFILE':
  54. $this->executePostFile($ch);
  55. break;
  56. case 'PUT':
  57. $this->executePut($ch);
  58. break;
  59. case 'DELETE':
  60. $this->executeDelete($ch);
  61. break;
  62. default:
  63. throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
  64. }
  65. }
  66. catch (InvalidArgumentException $e)
  67. {
  68. curl_close($ch);
  69. throw $e;
  70. }
  71. catch (Exception $e)
  72. {
  73. curl_close($ch);
  74. throw $e;
  75. }
  76. return $this->response;
  77. }
  78. protected function executeGet ($ch)
  79. {
  80. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: '.$this->acceptType, 'Content-Type: '.$this->contentType,'User-Agent: '.$this->userAgent));
  81. $this->doExecute($ch);
  82. }
  83. protected function executePost ($ch)
  84. {
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
  86. curl_setopt($ch, CURLOPT_POST, 1);
  87. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: '.$this->acceptType, 'Content-Type: '.$this->contentType, 'User-Agent: '.$this->userAgent));
  88. $this->doExecute($ch);
  89. }
  90. protected function executePostFile ($ch)
  91. {
  92. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
  93. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: '.$this->acceptType, 'Content-Type: '.$this->contentType,'User-Agent: '.$this->userAgent));
  94. curl_setopt($ch, CURLOPT_POST, 1);
  95. $this->doExecute($ch);
  96. }
  97. protected function executePut ($ch)
  98. {
  99. $this->requestLength = strlen($this->requestBody);
  100. $fh = fopen('php://memory', 'rw');
  101. fwrite($fh, $this->requestBody);
  102. rewind($fh);
  103. curl_setopt($ch, CURLOPT_INFILE, $fh);
  104. curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
  105. curl_setopt($ch, CURLOPT_PUT, true);
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: '.$this->acceptType, 'Content-Type: '.$this->contentType,'User-Agent: '.$this->userAgent));
  107. $this->doExecute($ch);
  108. fclose($fh);
  109. }
  110. protected function executeDelete ($ch)
  111. {
  112. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  113. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: '.$this->acceptType, 'Content-Type: '.$this->contentType));
  114. $this->doExecute($ch);
  115. }
  116. protected function doExecute (&$curlHandle)
  117. {
  118. $this->setCurlOpts($curlHandle);
  119. $response_str = curl_exec($curlHandle);
  120. $responseInfo = curl_getinfo($curlHandle);
  121. $header_str = substr($response_str,0,$responseInfo['header_size']);
  122. $content_str = substr($response_str,$responseInfo['header_size']);
  123. $this->response['content'] = json_decode($content_str, true);
  124. $this->response['info'] = $responseInfo;
  125. $this->response['http_code'] = $responseInfo['http_code'];
  126. $this->response['header'] = array();
  127. $lines = explode("\n",$header_str);
  128. foreach($lines as $line)
  129. {
  130. if (strpos($line, ": ") !== FALSE)
  131. {
  132. list($key,$value) = explode(": ",$line);
  133. $this->response['header'][$key] = trim($value);
  134. }
  135. }
  136. curl_close($curlHandle);
  137. }
  138. protected function setCurlOpts (&$curlHandle)
  139. {
  140. curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
  141. curl_setopt($curlHandle, CURLOPT_URL, $this->url);
  142. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  143. curl_setopt($curlHandle, CURLOPT_HEADER, true);
  144. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, !preg_match("!^https!i",$this->url));
  145. }
  146. protected function setAuth (&$curlHandle)
  147. {
  148. if ($this->username !== null && $this->password !== null)
  149. {
  150. curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  151. curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
  152. }
  153. }
  154. public function getAcceptType ()
  155. {
  156. return $this->acceptType;
  157. }
  158. public function setAcceptType ($acceptType)
  159. {
  160. $this->acceptType = $acceptType;
  161. }
  162. public function getPassword ()
  163. {
  164. return $this->password;
  165. }
  166. public function setPassword ($password)
  167. {
  168. $this->password = $password;
  169. }
  170. public function getResponseBody ()
  171. {
  172. return $this->responseBody;
  173. }
  174. public function getResponseInfo ()
  175. {
  176. return $this->responseInfo;
  177. }
  178. public function getUrl ()
  179. {
  180. return $this->url;
  181. }
  182. public function setUrl ($url)
  183. {
  184. $this->url = $url;
  185. }
  186. public function getUsername ()
  187. {
  188. return $this->username;
  189. }
  190. public function setUsername ($username)
  191. {
  192. $this->username = $username;
  193. }
  194. public function getVerb ()
  195. {
  196. return $this->verb;
  197. }
  198. public function setVerb ($verb)
  199. {
  200. $this->verb = $verb;
  201. }
  202. public function getRequestBody ()
  203. {
  204. return $this->requestBody;
  205. }
  206. public function setRequestBody ($body)
  207. {
  208. $this->requestBody = $body;
  209. }
  210. }
  211. ?>