Browse Source

return code as int

pull/1/head 0.1.3
Aaron Parecki 4 years ago
parent
commit
04a9a49245
No known key found for this signature in database GPG Key ID: 276C2817346D6056
4 changed files with 19 additions and 19 deletions
  1. +2
    -2
      src/p3k/HTTP.php
  2. +6
    -6
      src/p3k/HTTP/Curl.php
  3. +3
    -3
      src/p3k/HTTP/Stream.php
  4. +8
    -8
      src/p3k/HTTP/Test.php

+ 2
- 2
src/p3k/HTTP.php View File

@ -78,7 +78,7 @@ class HTTP {
}
private static function _parse_headers($headers) {
$retVal = array();
$retVal = [];
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
foreach($fields as $field) {
if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
@ -91,7 +91,7 @@ class HTTP {
}, strtolower(trim($match[1])));
if(isset($retVal[$match[1]])) {
if(!is_array($retVal[$match[1]]))
$retVal[$match[1]] = array($retVal[$match[1]]);
$retVal[$match[1]] = [$retVal[$match[1]]];
$retVal[$match[1]][] = $match[2];
} else {
$retVal[$match[1]] = trim($match[2]);

+ 6
- 6
src/p3k/HTTP/Curl.php View File

@ -22,7 +22,7 @@ class Curl implements Transport {
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_str = trim(substr($response, 0, $header_size));
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => $header_str,
'body' => substr($response, $header_size),
@ -30,7 +30,7 @@ class Curl implements Transport {
'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response
);
];
}
public function post($url, $body, $headers=[]) {
@ -43,7 +43,7 @@ class Curl implements Transport {
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_str = trim(substr($response, 0, $header_size));
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => $header_str,
'body' => substr($response, $header_size),
@ -51,7 +51,7 @@ class Curl implements Transport {
'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response
);
];
}
public function head($url, $headers=[]) {
@ -61,14 +61,14 @@ class Curl implements Transport {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => trim($response),
'error' => self::error_string_from_code(curl_errno($ch)),
'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response
);
];
}
private function _set_curlopts($ch, $url) {

+ 3
- 3
src/p3k/HTTP/Stream.php View File

@ -72,14 +72,14 @@ class Stream implements Transport {
];
}
return array(
return [
'code' => self::parse_response_code($http_response_header),
'header' => implode("\r\n", $http_response_header),
'body' => $body,
'error' => $error ? $error['code'] : false,
'error_description' => $error ? $error['description'] : false,
'url' => $url,
);
];
}
private function _stream_context($method, $url, $body=false, $headers=[]) {
@ -115,7 +115,7 @@ class Stream implements Transport {
$code = $match[1];
}
}
return $code;
return (int)$code;
}
}

+ 8
- 8
src/p3k/HTTP/Test.php View File

@ -43,14 +43,14 @@ class Test implements Transport {
public function head($url, $headers=[]) {
$response = $this->_read_file($url);
return array(
'code' => $response['code'],
return [
'code' => (int)$response['code'],
'headers' => $response['headers'],
'rels' => $response['rels'],
'error' => '',
'error_description' => '',
'url' => $response['url']
);
];
}
private function _read_file($url) {
@ -100,19 +100,19 @@ class Test implements Transport {
$effectiveUrl = $url;
}
return array(
'code' => $code,
return [
'code' => (int)$code,
'headers' => $parsedHeaders,
'rels' => \IndieWeb\http_rels($headers),
'body' => $body,
'error' => (isset($parsedHeaders['X-Test-Error']) ? $parsedHeaders['X-Test-Error'] : ''),
'error_description' => '',
'url' => $effectiveUrl
);
];
}
private static function _parse_headers($headers) {
$retVal = array();
$retVal = [];
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
foreach($fields as $field) {
if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
@ -125,7 +125,7 @@ class Test implements Transport {
}, strtolower(trim($match[1])));
if(isset($retVal[$match[1]])) {
if(!is_array($retVal[$match[1]]))
$retVal[$match[1]] = array($retVal[$match[1]]);
$retVal[$match[1]] = [$retVal[$match[1]]];
$retVal[$match[1]][] = $match[2];
} else {
$retVal[$match[1]] = trim($match[2]);

Loading…
Cancel
Save