/gulliver/system/class.restClient.php

https://bitbucket.org/ferOnti/processmaker · PHP · 349 lines · 179 code · 26 blank · 144 comment · 22 complexity · ee1511617317810ae754707c8af88174 MD5 · raw file

  1. <?php
  2. /**
  3. * Class RestClient
  4. */
  5. class RestClient
  6. {
  7. private $curl;
  8. private $url;
  9. private $response = "";
  10. private $headers = array ();
  11. private $method = "GET";
  12. private $params = null;
  13. private $contentType = null;
  14. private $file = null;
  15. /**
  16. * Private Constructor, sets default options
  17. */
  18. public function __construct ()
  19. {
  20. $this->curl = curl_init();
  21. curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true );
  22. curl_setopt( $this->curl, CURLOPT_AUTOREFERER, true ); // This make sure will follow redirects
  23. curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, true ); // This too
  24. curl_setopt( $this->curl, CURLOPT_HEADER, true ); // THis verbose option for extracting the headers
  25. }
  26. /**
  27. * Execute the call to the webservice
  28. *
  29. * @return RestClient
  30. */
  31. public function execute ()
  32. {
  33. if ($this->method === "POST") {
  34. curl_setopt( $this->curl, CURLOPT_POST, true );
  35. curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $this->params );
  36. //var_dump($this->params);
  37. //die;
  38. } elseif ($this->method == "GET") {
  39. curl_setopt( $this->curl, CURLOPT_HTTPGET, true );
  40. $this->treatURL();
  41. } elseif ($this->method === "PUT") {
  42. curl_setopt( $this->curl, CURLOPT_PUT, true );
  43. $this->treatURL();
  44. $this->file = tmpFile();
  45. fwrite( $this->file, $this->params );
  46. fseek( $this->file, 0 );
  47. curl_setopt( $this->curl, CURLOPT_INFILE, $this->file );
  48. curl_setopt( $this->curl, CURLOPT_INFILESIZE, strlen( $this->params ) );
  49. } else {
  50. curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, $this->method );
  51. }
  52. if ($this->contentType != null) {
  53. curl_setopt( $this->curl, CURLOPT_HTTPHEADER, array ("Content-Type: " . $this->contentType,"SKEY: 8QRtY5zXyViZ9fjYou" ) );
  54. }
  55. curl_setopt( $this->curl, CURLOPT_URL, $this->url );
  56. $r = curl_exec( $this->curl );
  57. if ($this->method !== "DELETE") {
  58. $this->treatResponse( $r ); // Extract the headers and response
  59. return $this;
  60. } else {
  61. return $this;
  62. }
  63. }
  64. /**
  65. * Treats URL
  66. */
  67. private function treatURL ()
  68. {
  69. if (is_array( $this->params ) && count( $this->params ) >= 1) {
  70. // Transform parameters in key/value pars in URL
  71. if (! strpos( $this->url, '?' )) {
  72. $this->url .= '?';
  73. }
  74. foreach ($this->params as $k => $v) {
  75. $this->url .= "&" . urlencode( $k ) . "=" . urlencode( $v );
  76. }
  77. }
  78. return $this->url;
  79. }
  80. /*
  81. * Treats the Response for extracting the Headers and Response
  82. */
  83. private function treatResponse ($r)
  84. {
  85. if ($r == null or strlen( $r ) < 1) {
  86. return;
  87. }
  88. $parts = explode( "\n\r", $r ); // HTTP packets define that Headers end in a blank line (\n\r) where starts the body
  89. while (preg_match( '@HTTP/1.[0-1] 100 Continue@', $parts[0] ) or preg_match( "@Moved@", $parts[0] )) {
  90. // Continue header must be bypass
  91. for ($i = 1; $i < count( $parts ); $i ++) {
  92. $parts[$i - 1] = trim( $parts[$i] );
  93. }
  94. unset( $parts[count( $parts ) - 1] );
  95. }
  96. preg_match( "@Content-Type: ([a-zA-Z0-9-]+/?[a-zA-Z0-9-]*)@", $parts[0], $reg ); // This extract the content type
  97. $this->headers['content-type'] = $reg[1];
  98. preg_match( "@HTTP/1.[0-1] ([0-9]{3}) ([a-zA-Z ]+)@", $parts[0], $reg ); // This extracts the response header Code and Message
  99. $this->headers['code'] = $reg[1];
  100. $this->headers['message'] = $reg[2];
  101. $this->response = "";
  102. for ($i = 1; $i < count( $parts ); $i ++) {
  103. //This make sure that exploded response get back togheter
  104. if ($i > 1) {
  105. $this->response .= "\n\r";
  106. }
  107. $this->response .= $parts[$i];
  108. }
  109. }
  110. /*
  111. * @return array
  112. */
  113. public function getHeaders ()
  114. {
  115. return $this->headers;
  116. }
  117. /*
  118. * @return string
  119. */
  120. public function getResponse ()
  121. {
  122. return $this->response;
  123. }
  124. /*
  125. * HTTP response code (404,401,200,etc)
  126. * @return int
  127. */
  128. public function getResponseCode ()
  129. {
  130. return (int) $this->headers['code'];
  131. }
  132. /*
  133. * HTTP response message (Not Found, Continue, etc )
  134. * @return string
  135. */
  136. public function getResponseMessage ()
  137. {
  138. return $this->headers['message'];
  139. }
  140. /*
  141. * Content-Type (text/plain, application/xml, etc)
  142. * @return string
  143. */
  144. public function getResponseContentType ()
  145. {
  146. return $this->headers['content-type'];
  147. }
  148. /**
  149. * This sets that will not follow redirects
  150. *
  151. * @return RestClient
  152. */
  153. public function setNoFollow ()
  154. {
  155. curl_setopt( $this->curl, CURLOPT_AUTOREFERER, false );
  156. curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, false );
  157. return $this;
  158. }
  159. /**
  160. * This closes the connection and release resources
  161. *
  162. * @return RestClient
  163. */
  164. public function close ()
  165. {
  166. curl_close( $this->curl );
  167. $this->curl = null;
  168. if ($this->file != null) {
  169. fclose( $this->file );
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Sets the URL to be Called
  175. *
  176. * @return RestClient
  177. */
  178. public function setUrl ($url)
  179. {
  180. $this->url = $url;
  181. return $this;
  182. }
  183. /**
  184. * Set the Content-Type of the request to be send
  185. * Format like "application/xml" or "text/plain" or other
  186. *
  187. * @param string $contentType
  188. * @return RestClient
  189. */
  190. public function setContentType ($contentType)
  191. {
  192. $this->contentType = $contentType;
  193. return $this;
  194. }
  195. /**
  196. * Set the Credentials for BASIC Authentication
  197. *
  198. * @param string $user
  199. * @param string $pass
  200. * @return RestClient
  201. */
  202. public function setCredentials ($user, $pass)
  203. {
  204. if ($user != null) {
  205. curl_setopt( $this->curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
  206. curl_setopt( $this->curl, CURLOPT_USERPWD, "{$user}:{$pass}" );
  207. }
  208. return $this;
  209. }
  210. /**
  211. * Set the Request HTTP Method
  212. * For now, only accepts GET and POST
  213. *
  214. * @param string $method
  215. * @return RestClient
  216. */
  217. public function setMethod ($method)
  218. {
  219. $this->method = $method;
  220. return $this;
  221. }
  222. /**
  223. * Set Parameters to be send on the request
  224. * It can be both a key/value par array (as in array("key"=>"value"))
  225. * or a string containing the body of the request, like a XML, JSON or other
  226. * Proper content-type should be set for the body if not a array
  227. *
  228. * @param mixed $params
  229. * @return RestClient
  230. */
  231. public function setParameters ($params)
  232. {
  233. $this->params = $params;
  234. return $this;
  235. }
  236. /**
  237. * Creates the RESTClient
  238. *
  239. * @param string $url=null [optional]
  240. * @return RestClient
  241. */
  242. public static function createClient ($url = null)
  243. {
  244. $client = new RestClient();
  245. if ($url != null) {
  246. $client->setUrl( $url );
  247. }
  248. return $client;
  249. }
  250. /**
  251. * Convenience method wrapping a commom POST call
  252. *
  253. * @param string $url
  254. * @param mixed params
  255. * @param string $user=null [optional]
  256. * @param string $password=null [optional]
  257. * @param string $contentType="multpary/form-data" [optional] commom post (multipart/form-data) as default
  258. * @return RestClient
  259. */
  260. //public static function post($url,$params=null,$user=null,$pwd=null,$contentType="multipart/form-data") {
  261. public static function post ($url, $params, $user, $pwd, $contentType = "multipart/form-data")
  262. {
  263. //return self::call("POST",$url,$params,$user,$pwd,$contentType);
  264. return self::call( "POST", $url, $params, $user, $pwd, $contentType );
  265. }
  266. /**
  267. * Convenience method wrapping a commom PUT call
  268. *
  269. * @param string $url
  270. * @param string $body
  271. * @param string $user=null [optional]
  272. * @param string $password=null [optional]
  273. * @param string $contentType=null [optional]
  274. * @return RestClient
  275. */
  276. public static function put ($url, $body, $user = null, $pwd = null, $contentType = null)
  277. {
  278. return self::call( "PUT", $url, $body, $user, $pwd, $contentType );
  279. }
  280. /**
  281. * Convenience method wrapping a commom GET call
  282. *
  283. * @param string $url
  284. * @param array params
  285. * @param string $user=null [optional]
  286. * @param string $password=null [optional]
  287. * @return RestClient
  288. */
  289. //public static function get($url,array $params=null,$user=null,$pwd=null,$contentType=null) {
  290. public static function get ($url, $user, $pwd, $contentType = null)
  291. {
  292. //return self::call("GET",$url,$params,$user,$pwd,$contentType);
  293. return self::call( "GET", $url, null, $user, $pwd, $contentType );
  294. }
  295. /**
  296. * Convenience method wrapping a commom delete call
  297. *
  298. * @param string $url
  299. * @param array params
  300. * @param string $user=null [optional]
  301. * @param string $password=null [optional]
  302. * @return RestClient
  303. */
  304. public static function delete ($url, $user = null, $pwd = null, $contentType = null)
  305. {
  306. return self::call( "DELETE", $url, null, $user, $pwd, $contentType );
  307. }
  308. /**
  309. * Convenience method wrapping a commom custom call
  310. *
  311. * @param string $method
  312. * @param string $url
  313. * @param string $body
  314. * @param string $user=null [optional]
  315. * @param string $password=null [optional]
  316. * @param string $contentType=null [optional]
  317. * @return RestClient
  318. */
  319. public static function call ($method, $url, $body, $user = null, $pwd = null, $contentType = null)
  320. {
  321. return self::createClient( $url )->setParameters( $body )->setMethod( $method )->setCredentials( $user, $pwd )->setContentType( $contentType )->execute()->close();
  322. }
  323. }