HTTP.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * [Request_Client_External] HTTP driver performs external requests using the
  4. * php-http extension. To use this driver, ensure the following is completed
  5. * before executing an external request- ideally in the application bootstrap.
  6. *
  7. * @example
  8. *
  9. * // In application bootstrap
  10. * Request_Client_External::$client = 'Request_Client_HTTP';
  11. *
  12. * @package Kohana
  13. * @category Base
  14. * @author Kohana Team
  15. * @copyright (c) Kohana Team
  16. * @license https://koseven.ga/LICENSE.md
  17. * @uses [PECL HTTP](http://php.net/manual/en/book.http.php)
  18. */
  19. class Kohana_Request_Client_HTTP extends Request_Client_External {
  20. /**
  21. * Creates a new `Request_Client` object,
  22. * allows for dependency injection.
  23. *
  24. * @param array $params Params
  25. * @throws Request_Exception
  26. */
  27. public function __construct(array $params = [])
  28. {
  29. // Check that PECL HTTP supports requests
  30. if ( ! http_support(HTTP_SUPPORT_REQUESTS))
  31. {
  32. throw new Request_Exception('Need HTTP request support!');
  33. }
  34. // Carry on
  35. parent::__construct($params);
  36. }
  37. /**
  38. * @var array curl options
  39. * @link http://www.php.net/manual/function.curl-setopt
  40. */
  41. protected $_options = [];
  42. /**
  43. * Sends the HTTP message [Request] to a remote server and processes
  44. * the response.
  45. *
  46. * @param Request $request request to send
  47. * @param Response $request response to send
  48. * @return Response
  49. */
  50. public function _send_message(Request $request, Response $response)
  51. {
  52. $http_method_mapping = [
  53. HTTP_Request::GET => HTTPRequest::METH_GET,
  54. HTTP_Request::HEAD => HTTPRequest::METH_HEAD,
  55. HTTP_Request::POST => HTTPRequest::METH_POST,
  56. HTTP_Request::PUT => HTTPRequest::METH_PUT,
  57. HTTP_Request::DELETE => HTTPRequest::METH_DELETE,
  58. HTTP_Request::OPTIONS => HTTPRequest::METH_OPTIONS,
  59. HTTP_Request::TRACE => HTTPRequest::METH_TRACE,
  60. HTTP_Request::CONNECT => HTTPRequest::METH_CONNECT,
  61. ];
  62. // Create an http request object
  63. $http_request = new HTTPRequest($request->uri(), $http_method_mapping[$request->method()]);
  64. if ($this->_options)
  65. {
  66. // Set custom options
  67. $http_request->setOptions($this->_options);
  68. }
  69. // Set headers
  70. $http_request->setHeaders($request->headers()->getArrayCopy());
  71. // Set cookies
  72. $http_request->setCookies($request->cookie());
  73. // Set query data (?foo=bar&bar=foo)
  74. $http_request->setQueryData($request->query());
  75. // Set the body
  76. if ($request->method() == HTTP_Request::PUT)
  77. {
  78. $http_request->addPutData($request->body());
  79. }
  80. else
  81. {
  82. $http_request->setBody($request->body());
  83. }
  84. try
  85. {
  86. $http_request->send();
  87. }
  88. catch (HTTPRequestException $e)
  89. {
  90. throw new Request_Exception($e->getMessage());
  91. }
  92. catch (HTTPMalformedHeaderException $e)
  93. {
  94. throw new Request_Exception($e->getMessage());
  95. }
  96. catch (HTTPEncodingException $e)
  97. {
  98. throw new Request_Exception($e->getMessage());
  99. }
  100. // Build the response
  101. $response->status($http_request->getResponseCode())
  102. ->headers($http_request->getResponseHeader())
  103. ->cookie($http_request->getResponseCookies())
  104. ->body($http_request->getResponseBody());
  105. return $response;
  106. }
  107. }