Browse Source

add HTTP PUT function

main 0.1.13
Aaron Parecki 2 months ago
parent
commit
ae4988ba19
5 changed files with 43 additions and 0 deletions
  1. +11
    -0
      src/p3k/HTTP.php
  2. +21
    -0
      src/p3k/HTTP/Curl.php
  3. +6
    -0
      src/p3k/HTTP/Stream.php
  4. +4
    -0
      src/p3k/HTTP/Test.php
  5. +1
    -0
      src/p3k/HTTP/Transport.php

+ 11
- 0
src/p3k/HTTP.php View File

@ -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']);

+ 21
- 0
src/p3k/HTTP/Curl.php View File

@ -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);

+ 6
- 0
src/p3k/HTTP/Stream.php View File

@ -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);

+ 4
- 0
src/p3k/HTTP/Test.php View File

@ -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 [

+ 1
- 0
src/p3k/HTTP/Transport.php View File

@ -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);

Loading…
Cancel
Save