/workflow/engine/methods/services/Rest/CURLMessage.php

https://bitbucket.org/ferOnti/processmaker · PHP · 156 lines · 89 code · 19 blank · 48 comment · 4 complexity · 9627f693042c0c629f887c893981f742 MD5 · raw file

  1. <?php
  2. /**
  3. * Abstract class containing the CURL functionality, used to handle GET, POST, PUT and DELETE http methods.
  4. *
  5. * This class uses many different Curl functions, it would be great if this one gorws to allow the use of alll of them.
  6. *
  7. * @category Zend
  8. * @package ProcessMaker
  9. * @subpackage workflow
  10. * @copyright Copyright (c) ProcessMaker Colosa Inc.
  11. * @version Release: @2.0.44@
  12. * @since Class available since Release 2.0.44
  13. */
  14. define( 'PATH_SEP', '/' );
  15. define( 'COLON', ':' );
  16. define( 'DOT', '.' );
  17. define( 'PROTOCOL_HTTP', 'http' );
  18. /**
  19. * Abstract class, containing the CURL functionality, used to handle GET, POST, PUT and DELETE http methods.
  20. */
  21. abstract class CURLMessage
  22. {
  23. protected $restServer; // e.g. "http://ralph.pmos.colosa.net/rest/ralph/"; host + technich dir + workspace
  24. protected $content = "Content-Type: application/"; //set the string used to attach next the kind of message to be handle.
  25. protected $serviceTechnic = "rest"; // name of the current durectory where the rest methods are.
  26. protected $server_method; // used to set the name of the remote host and the Rest method to be called.
  27. protected $type; // contains the type of the message.
  28. protected $ch; //curl handle instance.
  29. protected $output; // contains the output returned by the curl_exec function.
  30. /**
  31. * Setting the remote host and init the Curl handle options
  32. */
  33. function __construct ()
  34. {
  35. $serverDNS = explode( DOT, $_SERVER['SERVER_NAME'] );
  36. $serverDNS = array_reverse( $serverDNS );
  37. $workspace = array_pop( $serverDNS ); //***aware this must contains the workspace name***
  38. $this->restServer = PROTOCOL_HTTP . COLON . PATH_SEP . PATH_SEP;
  39. $this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP;
  40. $this->restServer .= $this->serviceTechnic . PATH_SEP . $workspace . PATH_SEP;
  41. $this->ch = curl_init();
  42. curl_setopt( $this->ch, CURLOPT_TIMEOUT, 2 );
  43. curl_setopt( $this->ch, CURLOPT_POST, 1 );
  44. curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );
  45. }
  46. /**
  47. * set the message in order to follow the message format
  48. */
  49. abstract protected function format (array $message);
  50. /**
  51. * Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg.
  52. */
  53. abstract protected function setMoreProperties ($messageFormated);
  54. /**
  55. * Attach the method to the restServer path, set the type of the message, and the message itself.
  56. */
  57. protected function setMessageProperties ($method, array $message)
  58. {
  59. $messageFormated = $this->format( $message );
  60. $this->server_method = $this->restServer . $method;
  61. $this->setMoreProperties( $messageFormated );
  62. }
  63. /**
  64. * Send or execute(curl notation) the message using a rest method
  65. */
  66. public function send ($method, array $message)
  67. {
  68. self::setMessageProperties( $method, $message );
  69. $this->output = curl_exec( $this->ch );
  70. return $this->output;
  71. }
  72. /**
  73. * Set the message to GET method type
  74. */
  75. public function sendGET ($method, array $message)
  76. {
  77. curl_setopt( $this->ch, CURLOPT_HTTPGET, true );
  78. return $this->send( $method, $message );
  79. }
  80. /**
  81. * Set the message to POST method type
  82. */
  83. public function sendPOST ($method, array $message)
  84. {
  85. curl_setopt( $this->ch, CURLOPT_POST, true );
  86. return $this->send( $method, $message );
  87. }
  88. /**
  89. * Set the message to PUT method type
  90. */
  91. public function sendPUT ($method, array $message)
  92. {
  93. curl_setopt( $this->ch, CURLOPT_PUT, true );
  94. return $this->send( $method, $message );
  95. }
  96. /**
  97. * Set the message to DELETE method type
  98. */
  99. public function sendDELETE ($method, array $message)
  100. {
  101. curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
  102. return $this->send( $method, $message );
  103. }
  104. /**
  105. * Display all the data that the response could got.
  106. */
  107. public function displayResponse ()
  108. {
  109. $error = curl_error( $this->ch );
  110. $result = array ('header' => '','body' => '','curl_error' => '','http_code' => '','last_url' => ''
  111. );
  112. if ($error != "") {
  113. $result['curl_error'] = $error;
  114. return $result;
  115. }
  116. $response = $this->output;
  117. $header_size = curl_getinfo( $this->ch, CURLINFO_HEADER_SIZE );
  118. $result['header'] = substr( $response, 0, $header_size );
  119. $result['body'] = substr( $response, $header_size );
  120. $result['http_code'] = curl_getinfo( $this->ch, CURLINFO_HTTP_CODE );
  121. $result['last_url'] = curl_getinfo( $this->ch, CURLINFO_EFFECTIVE_URL );
  122. echo $this->type . " Response: " . $response . "<BR>";
  123. foreach ($result as $index => $data) {
  124. if ($data != "") {
  125. echo $index . "=" . $data . "<BR>";
  126. }
  127. }
  128. echo "<BR>";
  129. }
  130. /**
  131. * Close the Curl session using the Curl Handle set by curl_init() function.
  132. */
  133. public function close ()
  134. {
  135. curl_close( $this->ch );
  136. }
  137. }