Curl.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * [Request_Client_External] Curl driver performs external requests using the
  4. * php-curl extention. This is the default driver for all external requests.
  5. *
  6. * @package Kohana
  7. * @category Base
  8. * @author Kohana Team
  9. * @copyright (c) Kohana Team
  10. * @license https://koseven.ga/LICENSE.md
  11. * @uses [PHP cURL](http://php.net/manual/en/book.curl.php)
  12. */
  13. class Kohana_Request_Client_Curl extends Request_Client_External {
  14. /**
  15. * Sends the HTTP message [Request] to a remote server and processes
  16. * the response.
  17. *
  18. * @param Request $request request to send
  19. * @param Response $request response to send
  20. * @return Response
  21. */
  22. public function _send_message(Request $request, Response $response)
  23. {
  24. $options = [];
  25. // Set the request method
  26. $options = $this->_set_curl_request_method($request, $options);
  27. // Set the request body. This is perfectly legal in CURL even
  28. // if using a request other than POST. PUT does support this method
  29. // and DOES NOT require writing data to disk before putting it, if
  30. // reading the PHP docs you may have got that impression. SdF
  31. // This will also add a Content-Type: application/x-www-form-urlencoded header unless you override it
  32. if ($body = $request->body()) {
  33. $options[CURLOPT_POSTFIELDS] = $body;
  34. }
  35. // Process headers
  36. if ($headers = $request->headers())
  37. {
  38. $http_headers = [];
  39. foreach ($headers as $key => $value)
  40. {
  41. $http_headers[] = $key.': '.$value;
  42. }
  43. $options[CURLOPT_HTTPHEADER] = $http_headers;
  44. }
  45. // Process cookies
  46. if ($cookies = $request->cookie())
  47. {
  48. $options[CURLOPT_COOKIE] = http_build_query($cookies, '', '; ');
  49. }
  50. // Get any exisiting response headers
  51. $response_header = $response->headers();
  52. // Implement the standard parsing parameters
  53. $options[CURLOPT_HEADERFUNCTION] = [$response_header, 'parse_header_string'];
  54. $this->_options[CURLOPT_RETURNTRANSFER] = TRUE;
  55. $this->_options[CURLOPT_HEADER] = FALSE;
  56. // Apply any additional options set to
  57. $options += $this->_options;
  58. $uri = $request->uri();
  59. if ($query = $request->query())
  60. {
  61. $uri .= '?'.http_build_query($query, '', '&');
  62. }
  63. // Open a new remote connection
  64. $curl = curl_init($uri);
  65. // Set connection options
  66. if ( ! curl_setopt_array($curl, $options))
  67. {
  68. throw new Request_Exception('Failed to set CURL options, check CURL documentation: :url',
  69. [':url' => 'http://php.net/curl_setopt_array']);
  70. }
  71. // Get the response body
  72. $body = curl_exec($curl);
  73. // Get the response information
  74. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  75. if ($body === FALSE)
  76. {
  77. $error = curl_error($curl);
  78. }
  79. // Close the connection
  80. curl_close($curl);
  81. if (isset($error))
  82. {
  83. throw new Request_Exception('Error fetching remote :url [ status :code ] :error',
  84. [':url' => $request->url(), ':code' => $code, ':error' => $error]);
  85. }
  86. $response->status($code)
  87. ->body($body);
  88. return $response;
  89. }
  90. /**
  91. * Sets the appropriate curl request options. Uses the responding option
  92. * for POST or CURLOPT_CUSTOMREQUEST otherwise
  93. *
  94. * @param Request $request
  95. * @param array $options
  96. * @return array
  97. */
  98. public function _set_curl_request_method(Request $request, array $options)
  99. {
  100. switch ($request->method()) {
  101. case Request::POST:
  102. $options[CURLOPT_POST] = TRUE;
  103. break;
  104. default:
  105. $options[CURLOPT_CUSTOMREQUEST] = $request->method();
  106. break;
  107. }
  108. return $options;
  109. }
  110. }