PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Classes/routeurs/RouteurEdatisCurl.php

https://bitbucket.org/hfab/webservice
PHP | 157 lines | 117 code | 28 blank | 12 comment | 8 complexity | e409eb55bb5737e6f354f038448a8b21 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, MIT, GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php namespace App\Classes\Routeurs;
  2. class RouteurEdatisCurl
  3. {
  4. public $headers = array();
  5. public $options = array();
  6. protected $_Username;
  7. protected $_Password;
  8. private $Params = array();
  9. protected $handle;
  10. public function __construct()
  11. {
  12. }
  13. public function AddParam( $Key , $Value )
  14. {
  15. if( trim($Key == '') ) {
  16. return;
  17. }
  18. $this->Params[$Key] = $Value;
  19. }
  20. public function SetParams( $Params )
  21. {
  22. // $this->Params = array_merge( $this->Params, $Params );
  23. $this->Params = $Params;
  24. }
  25. public function AddHeader($Key, $Value)
  26. {
  27. $this->headers[$Key] = $Value;
  28. }
  29. public function delete( $url )
  30. {
  31. return $this->request('DELETE', $url, $this->Params );
  32. }
  33. public function get( $url )
  34. {
  35. if ( ! empty( $this->Params ) ) {
  36. $url .= (stripos($url, '?') !== false) ? '&' : '?';
  37. $url .= http_build_query($this->Params, '', '&');
  38. }
  39. return $this->request('GET', $url);
  40. }
  41. public function post( $url )
  42. {
  43. return $this->request('POST', $url, $this->Params);
  44. }
  45. public function put( $url )
  46. {
  47. return $this->request('PUT', $url, $this->Params);
  48. }
  49. protected function request($method, $url, $vars = array())
  50. {
  51. $this->handle = curl_init();
  52. # Set some default CURL options
  53. curl_setopt($this->handle, CURLOPT_HEADER, true);
  54. curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
  55. curl_setopt($this->handle, CURLOPT_URL, $url);
  56. curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, 0);
  57. # Determine the request method and set the correct CURL option
  58. switch ($method) {
  59. case 'GET':
  60. curl_setopt($this->handle, CURLOPT_HTTPGET, true);
  61. break;
  62. case ('POST' || 'DELETE' || 'PUT'):
  63. curl_setopt($this->handle, CURLOPT_POST, true);
  64. $params = json_encode($vars);
  65. $json = (is_array($params) ? http_build_query($params) : $params);
  66. curl_setopt($this->handle, CURLOPT_POSTFIELDS, $json);
  67. $this->AddHeader('Content-Length', strlen($json));
  68. break;
  69. }
  70. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, strtoupper($method));
  71. # Format custom headers for this request and set CURL option
  72. $headers = array();
  73. foreach ($this->headers as $key => $value)
  74. {
  75. $headers[] = $key.': '.$value;
  76. }
  77. curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
  78. # Set any custom CURL options
  79. foreach ($this->options as $option => $value)
  80. {
  81. curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
  82. }
  83. $response = curl_exec($this->handle);
  84. // var_dump('La rep : [' . $response . ']');
  85. // \Log::info($response);
  86. if ( $response !== false ) {
  87. $response = new CurlResponseEdatis($response);
  88. } else {
  89. // throw new CurlExceptionEdatis( curl_errno($this->handle).' - '.curl_error($this->handle) );
  90. \Log::error(curl_errno($this->handle).' - '.curl_error($this->handle));
  91. return false;
  92. }
  93. curl_close($this->handle);
  94. return $response;
  95. }
  96. }
  97. class CurlExceptionEdatis extends \Exception
  98. {
  99. }
  100. class CurlResponseEdatis
  101. {
  102. public $body = '';
  103. public $headers = array();
  104. public function __construct( $response )
  105. {
  106. # Extract headers from response
  107. $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
  108. preg_match_all($pattern, $response, $matches);
  109. $headers = explode("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
  110. # Extract the version and status from the first header
  111. $version_and_status = array_shift($headers);
  112. preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
  113. $this->headers['Http-Version'] = $matches[1];
  114. $this->headers['Status-Code'] = $matches[2];
  115. $this->headers['Status'] = $matches[2].' '.$matches[3];
  116. # Convert headers into an associative array
  117. foreach ($headers as $header)
  118. {
  119. preg_match('#(.*?)\:\s(.*)#', $header, $matches);
  120. $this->headers[$matches[1]] = $matches[2];
  121. }
  122. # Remove the headers from the response body
  123. $this->body = preg_replace($pattern, '', $response);
  124. }
  125. public function __toString()
  126. {
  127. return $this->body;
  128. }
  129. }