/panada/Resources/Rest.php

https://github.com/back2arie/Panada · PHP · 230 lines · 112 code · 35 blank · 83 comment · 19 complexity · 24fd379bff76634315ef5777949d5f6e MD5 · raw file

  1. <?php
  2. /**
  3. * Panada Restful class.
  4. *
  5. * @package Resources
  6. * @link http://panadaframework.com/
  7. * @license http://www.opensource.org/licenses/bsd-license.php
  8. * @author Iskandar Soesman <k4ndar@yahoo.com>
  9. * @since Version 0.1
  10. */
  11. namespace Resources;
  12. class Rest
  13. {
  14. public
  15. $requestMethod,
  16. $responseStatus,
  17. $requestData = array(),
  18. $setRequestHeaders = array(),
  19. $responseOutputHeader = false,
  20. $timeout = 30,
  21. $curlSSLVerifypeer = true;
  22. public function __construct()
  23. {
  24. /**
  25. * Makesure Curl extension is enabled
  26. */
  27. if( ! extension_loaded('curl') )
  28. throw new RunException('Curl extension that required by Rest Resource is not available.');
  29. // Get the client request method
  30. $this->requestMethod = strtoupper($_SERVER['REQUEST_METHOD']);
  31. }
  32. /**
  33. * Getter for requstMethod
  34. *
  35. * @return string
  36. */
  37. public function getRequestMethod()
  38. {
  39. return $this->requestMethod;
  40. }
  41. /**
  42. * Getter for responseStatus
  43. *
  44. * @return int
  45. */
  46. public function getResponseStatus()
  47. {
  48. return $this->responseStatus;
  49. }
  50. /**
  51. * Getter for requestData
  52. *
  53. * @return array
  54. */
  55. public function getRequestData()
  56. {
  57. return $this->requestData;
  58. }
  59. /**
  60. * Get clent HTTP Request type.
  61. *
  62. * @return array
  63. */
  64. public function getRequest()
  65. {
  66. // Use PHP Input to get request PUT, DELETE, HEAD, TRACE, OPTIONS, CONNECT and PATCH
  67. return $_REQUEST;
  68. }
  69. /**
  70. * Get clent file(s) submited by POST or PUT.
  71. *
  72. * @return array
  73. */
  74. public function getFiles()
  75. {
  76. return $_FILES;
  77. }
  78. /**
  79. * Get client request headers
  80. *
  81. * @return array
  82. */
  83. public function getClientHeaders()
  84. {
  85. $headers = array();
  86. foreach ($_SERVER as $key => $val){
  87. if (substr($key, 0, 5) == 'HTTP_'){
  88. $key = str_replace('_', ' ', substr($key, 5));
  89. $key = str_replace(' ', '-', ucwords(strtolower($key)));
  90. $headers[$key] = $val;
  91. }
  92. }
  93. return $headers;
  94. }
  95. /**
  96. * See this trick at http://www.php.net/manual/en/function.curl-setopt.php#96056
  97. *
  98. * @return array
  99. */
  100. private function getPHPInput()
  101. {
  102. parse_str(file_get_contents('php://input'), $put_vars);
  103. return $put_vars;
  104. }
  105. /**
  106. * Set additional HTTP Request headers
  107. *
  108. * @param array $options
  109. * @return void
  110. */
  111. public function setRequestHeaders( $options = array() )
  112. {
  113. if( ! empty($options) )
  114. foreach($options as $key => $value)
  115. $this->setRequestHeaders[] = $key.': '.$value;
  116. }
  117. /**
  118. * Set HTTP Headers Authorization
  119. *
  120. * @param string $signature Secret string to access the server
  121. * @param string $type The Auth type eg: Basic, OAuth etc
  122. * @return void
  123. */
  124. public function setRequestAuthorization($signature, $type = 'Basic')
  125. {
  126. $this->setRequestHeaders[] = 'Authorization: '.$type.' '.$signature;
  127. }
  128. /**
  129. * Send HTTP Request to server
  130. *
  131. * @param string $uri The server's URL
  132. * @param string $method HTTP Request method type
  133. * @param array | string $data The data that need to send to server
  134. * @return booeal if false and string if true
  135. */
  136. public function sendRequest( $uri, $method = 'GET', $data = null )
  137. {
  138. $this->setRequestHeaders[] = 'User-Agent: Panada PHP Framework REST API/0.2';
  139. $method = strtoupper($method);
  140. $urlSeparator = ( parse_url( $uri, PHP_URL_QUERY ) ) ? '&' : '?';
  141. $uri = ( $method == 'GET' && ! empty($data) ) ? $uri . $urlSeparator . (is_array($data) ? http_build_query($data) : $data) : $uri;
  142. $c = curl_init();
  143. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  144. curl_setopt($c, CURLOPT_URL, $uri);
  145. curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
  146. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, $this->curlSSLVerifypeer);
  147. if($this->responseOutputHeader)
  148. curl_setopt($c, CURLOPT_HEADER, true);
  149. if( $method != 'GET' ) {
  150. if( is_array($data) )
  151. $data = http_build_query($data);
  152. if( $method == 'POST' )
  153. curl_setopt($c, CURLOPT_POST, true);
  154. if( $method == 'PUT' || $method == 'DELETE' ) {
  155. $this->setRequestHeaders[] = 'Content-Length: ' . strlen($data);
  156. curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
  157. }
  158. curl_setopt($c, CURLOPT_POSTFIELDS, $data);
  159. }
  160. curl_setopt($c, CURLOPT_HTTPHEADER, $this->setRequestHeaders);
  161. curl_setopt($c, CURLINFO_HEADER_OUT, true);
  162. $contents = curl_exec($c);
  163. $this->responseStatus = curl_getinfo($c, CURLINFO_HTTP_CODE);
  164. $this->headerSent = curl_getinfo($c, CURLINFO_HEADER_OUT);
  165. curl_close($c);
  166. if($contents)
  167. return $contents;
  168. return false;
  169. }
  170. /**
  171. * Set HTTP Response headers code
  172. *
  173. * @return void
  174. */
  175. public function setResponseHeader($code = 200)
  176. {
  177. Tools::setStatusHeader($code);
  178. }
  179. /**
  180. * The return data type
  181. *
  182. * @param array $data
  183. * @param string $format
  184. * @param string $ContentType
  185. * @return string
  186. */
  187. public function wrapResponseOutput($data, $format = 'json', $ContentType = 'application')
  188. {
  189. header('Content-type: '.$ContentType.'/' . $format);
  190. if($format == 'xml')
  191. return Tools::xmlEncode($data);
  192. else
  193. return json_encode($data);
  194. }
  195. }