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

/common/libraries/php/webservice/rest/client/client/curl_rest_client.class.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 157 lines | 118 code | 32 blank | 7 comment | 17 complexity | a3a40b3cbada6210b29cb645052e61fc MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. class CurlRestClient extends RestClient
  4. {
  5. private $curl;
  6. function __construct($base_url)
  7. {
  8. parent :: __construct($base_url);
  9. $this->set_mode(self :: MODE_CURL);
  10. $this->set_check_target_certificate(false);
  11. }
  12. function request($response_mime_type = false)
  13. {
  14. $url_info = parse_url($this->get_resource_url());
  15. if (isset($url_info['port']))
  16. {
  17. $url = $url_info['scheme'] . '://' . $url_info['host'] . $url_info['path'];
  18. if (isset($url_info['query']) && strlen($url_info['query']) > 0)
  19. {
  20. $url .= '?' . $url_info['query'];
  21. }
  22. $result->set_request_port($url_info['port']);
  23. $this->curl = curl_init($url);
  24. curl_setopt($this->curl, CURLOPT_PORT, $url_info['port']);
  25. }
  26. else
  27. {
  28. $url = $this->get_resource_url();
  29. $this->curl = curl_init($url);
  30. }
  31. curl_setopt($this->curl, CURLOPT_USERAGENT, 'Chamilo2Bot/1.0');
  32. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->get_method());
  33. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  34. if ($this->get_check_target_certificate())
  35. {
  36. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2);
  37. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, true);
  38. }
  39. else
  40. {
  41. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
  42. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
  43. }
  44. if ($this->has_authentication())
  45. {
  46. $this->get_authentication()->authenticate();
  47. }
  48. if ($this->has_data())
  49. {
  50. curl_setopt($this->curl, CURLOPT_POST, 1);
  51. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->get_data()->prepare());
  52. }
  53. if ($this->has_data_mimetype())
  54. {
  55. $this->add_header('Content-type', $this->get_data_mimetype());
  56. }
  57. if (count($this->get_headers()) > 0)
  58. {
  59. curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->get_headers());
  60. }
  61. //cookies
  62. $this->cookie_file = Path :: get_temp_path() . 'curl_cookies.txt';
  63. if (!\file_exists($this->cookie_file))
  64. {
  65. fopen($this->cookie_file, 'w') or die('cannot create coofie file');
  66. }
  67. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  68. curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookie_file);
  69. curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookie_file);
  70. $response_content = curl_exec($this->curl);
  71. $response_http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  72. $response_content_type = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE);
  73. if (! $response_mime_type)
  74. {
  75. $response_mime_type = self :: extract_mime_type($response_content_type);
  76. }
  77. $response_error = curl_error($this->curl);
  78. $result = RestResult :: content_type_factory($response_mime_type);
  79. $result->set_request_url($url);
  80. $result->set_request_method($this->get_method());
  81. $result->set_request_data($this->get_data());
  82. $result->set_response_content($response_content);
  83. $result->set_response_code($response_http_code);
  84. $result->set_response_cookies($this->parse_cookie_file());
  85. if (isset($response_error) && strlen($response_error) > 0)
  86. {
  87. $result->set_response_error($response_error);
  88. }
  89. elseif ($response_http_code < 200 || $response_http_code >= 300)
  90. {
  91. $result->set_response_error('The REST request returned an HTTP error code of ' . $response_http_code . ' (' . $this->get_http_code_translation($response_http_code) . ')');
  92. }
  93. curl_close($this->curl);
  94. return $result;
  95. }
  96. function parse_cookie_file()
  97. {
  98. $aCookies = array();
  99. $aLines = file($this->cookie_file);
  100. foreach ($aLines as $line)
  101. {
  102. if ('#' == $line{0})
  103. continue;
  104. $arr = explode("\t", $line);
  105. if (isset($arr[5]) && isset($arr[6]))
  106. $aCookies[$arr[5]] = $arr[6];
  107. }
  108. return $aCookies;
  109. }
  110. /**
  111. * @return the $curl
  112. */
  113. public function get_curl()
  114. {
  115. return $this->curl;
  116. }
  117. /**
  118. * @param $curl the $curl to set
  119. */
  120. public function set_curl($curl)
  121. {
  122. $this->curl = $curl;
  123. }
  124. }
  125. ?>