diff --git a/src/p3k/HTTP.php b/src/p3k/HTTP.php index 7ae2d35..487c5f9 100644 --- a/src/p3k/HTTP.php +++ b/src/p3k/HTTP.php @@ -69,6 +69,17 @@ class HTTP { return $response; } + public function put($url, $body, $headers=[]) { + $this->_transport->set_timeout($this->_timeout); + $this->_transport->set_max_redirects($this->_max_redirects); + if($this->_user_agent) { + $headers[] = 'User-Agent: ' . $this->_user_agent; + } + $response = $this->_transport->put($url, $body, $headers); + $response = $this->_build_response($response); + return $response; + } + private function _build_response($response) { // Parses the HTTP headers and adds the "headers" and "rels" response keys $response['headers'] = self::_parse_headers($response['header']); diff --git a/src/p3k/HTTP/Curl.php b/src/p3k/HTTP/Curl.php index 5eb889e..8b25774 100644 --- a/src/p3k/HTTP/Curl.php +++ b/src/p3k/HTTP/Curl.php @@ -55,6 +55,27 @@ class Curl implements Transport { ]; } + public function put($url, $body, $headers=[]) { + $ch = curl_init($url); + $this->_set_curlopts($ch, $url); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + if($headers) + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $header_str = trim(substr($response, 0, $header_size)); + return [ + 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), + 'header' => $header_str, + 'body' => substr($response, $header_size), + 'error' => self::error_string_from_code(curl_errno($ch)), + 'error_description' => curl_error($ch), + 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), + 'debug' => $response + ]; + } + public function head($url, $headers=[]) { $ch = curl_init($url); $this->_set_curlopts($ch, $url); diff --git a/src/p3k/HTTP/Stream.php b/src/p3k/HTTP/Stream.php index 70d070f..4dc7f5f 100644 --- a/src/p3k/HTTP/Stream.php +++ b/src/p3k/HTTP/Stream.php @@ -34,6 +34,12 @@ class Stream implements Transport { return $this->_fetch($url, $context); } + public function put($url, $body, $headers=[]) { + set_error_handler("p3k\HTTP\Stream::exception_error_handler"); + $context = $this->_stream_context('PUT', $url, $body, $headers); + return $this->_fetch($url, $context); + } + public function head($url, $headers=[]) { set_error_handler("p3k\HTTP\Stream::exception_error_handler"); $context = $this->_stream_context('HEAD', $url, false, $headers); diff --git a/src/p3k/HTTP/Test.php b/src/p3k/HTTP/Test.php index f81620b..c391b7a 100644 --- a/src/p3k/HTTP/Test.php +++ b/src/p3k/HTTP/Test.php @@ -41,6 +41,10 @@ class Test implements Transport { return $this->_read_file($url); } + public function put($url, $body, $headers=[]) { + return $this->_read_file($url); + } + public function head($url, $headers=[]) { $response = $this->_read_file($url); return [ diff --git a/src/p3k/HTTP/Transport.php b/src/p3k/HTTP/Transport.php index 9dd135a..5e750ff 100644 --- a/src/p3k/HTTP/Transport.php +++ b/src/p3k/HTTP/Transport.php @@ -26,6 +26,7 @@ interface Transport { public function get($url, $headers=[]); public function post($url, $body, $headers=[]); + public function put($url, $body, $headers=[]); public function head($url, $headers=[]); public function set_timeout($timeout);