Curl.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  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) 2008-2012 Kohana Team
  10. * @license http://kohanaframework.org/license
  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. // Response headers
  25. $response_headers = array();
  26. $options = array();
  27. // Set the request method
  28. $options = $this->_set_curl_request_method($request, $options);
  29. // Set the request body. This is perfectly legal in CURL even
  30. // if using a request other than POST. PUT does support this method
  31. // and DOES NOT require writing data to disk before putting it, if
  32. // reading the PHP docs you may have got that impression. SdF
  33. $options[CURLOPT_POSTFIELDS] = $request->body();
  34. // Process headers
  35. if ($headers = $request->headers())
  36. {
  37. $http_headers = array();
  38. foreach ($headers as $key => $value)
  39. {
  40. $http_headers[] = $key.': '.$value;
  41. }
  42. $options[CURLOPT_HTTPHEADER] = $http_headers;
  43. }
  44. // Process cookies
  45. if ($cookies = $request->cookie())
  46. {
  47. $options[CURLOPT_COOKIE] = http_build_query($cookies, NULL, '; ');
  48. }
  49. // Get any exisiting response headers
  50. $response_header = $response->headers();
  51. // Implement the standard parsing parameters
  52. $options[CURLOPT_HEADERFUNCTION] = array($response_header, 'parse_header_string');
  53. $this->_options[CURLOPT_RETURNTRANSFER] = TRUE;
  54. $this->_options[CURLOPT_HEADER] = FALSE;
  55. // Apply any additional options set to
  56. $options += $this->_options;
  57. $uri = $request->uri();
  58. if ($query = $request->query())
  59. {
  60. $uri .= '?'.http_build_query($query, NULL, '&');
  61. }
  62. // Open a new remote connection
  63. $curl = curl_init($uri);
  64. // Set connection options
  65. if ( ! curl_setopt_array($curl, $options))
  66. {
  67. throw new Request_Exception('Failed to set CURL options, check CURL documentation: :url',
  68. array(':url' => 'http://php.net/curl_setopt_array'));
  69. }
  70. // Get the response body
  71. $body = curl_exec($curl);
  72. // Get the response information
  73. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  74. if ($body === FALSE)
  75. {
  76. $error = curl_error($curl);
  77. }
  78. // Close the connection
  79. curl_close($curl);
  80. if (isset($error))
  81. {
  82. throw new Request_Exception('Error fetching remote :url [ status :code ] :error',
  83. array(':url' => $request->url(), ':code' => $code, ':error' => $error));
  84. }
  85. $response->status($code)
  86. ->body($body);
  87. return $response;
  88. }
  89. /**
  90. * Sets the appropriate curl request options. Uses the responding options
  91. * for POST and PUT, uses CURLOPT_CUSTOMREQUEST otherwise
  92. * @param Request $request
  93. * @param array $options
  94. * @return array
  95. */
  96. public function _set_curl_request_method(Request $request, array $options)
  97. {
  98. switch ($request->method()) {
  99. case Request::POST:
  100. $options[CURLOPT_POST] = TRUE;
  101. break;
  102. case Request::PUT:
  103. $options[CURLOPT_PUT] = TRUE;
  104. break;
  105. default:
  106. $options[CURLOPT_CUSTOMREQUEST] = $request->method();
  107. break;
  108. }
  109. return $options;
  110. }
  111. } // End Kohana_Request_Client_Curl