Browse Source

return code as int

pull/1/head 0.1.3
Aaron Parecki 7 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) { private static function _parse_headers($headers) {
$retVal = array();
$retVal = [];
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers)); $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
foreach($fields as $field) { foreach($fields as $field) {
if(preg_match('/([^:]+): (.+)/m', $field, $match)) { if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
@ -91,7 +91,7 @@ class HTTP {
}, strtolower(trim($match[1]))); }, strtolower(trim($match[1])));
if(isset($retVal[$match[1]])) { if(isset($retVal[$match[1]])) {
if(!is_array($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]; $retVal[$match[1]][] = $match[2];
} else { } else {
$retVal[$match[1]] = trim($match[2]); $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); $response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_str = trim(substr($response, 0, $header_size)); $header_str = trim(substr($response, 0, $header_size));
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => $header_str, 'header' => $header_str,
'body' => substr($response, $header_size), 'body' => substr($response, $header_size),
@ -30,7 +30,7 @@ class Curl implements Transport {
'error_description' => curl_error($ch), 'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response 'debug' => $response
);
];
} }
public function post($url, $body, $headers=[]) { public function post($url, $body, $headers=[]) {
@ -43,7 +43,7 @@ class Curl implements Transport {
$response = curl_exec($ch); $response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_str = trim(substr($response, 0, $header_size)); $header_str = trim(substr($response, 0, $header_size));
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => $header_str, 'header' => $header_str,
'body' => substr($response, $header_size), 'body' => substr($response, $header_size),
@ -51,7 +51,7 @@ class Curl implements Transport {
'error_description' => curl_error($ch), 'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response 'debug' => $response
);
];
} }
public function head($url, $headers=[]) { public function head($url, $headers=[]) {
@ -61,14 +61,14 @@ class Curl implements Transport {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch); $response = curl_exec($ch);
return array(
return [
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'header' => trim($response), 'header' => trim($response),
'error' => self::error_string_from_code(curl_errno($ch)), 'error' => self::error_string_from_code(curl_errno($ch)),
'error_description' => curl_error($ch), 'error_description' => curl_error($ch),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
'debug' => $response 'debug' => $response
);
];
} }
private function _set_curlopts($ch, $url) { 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), 'code' => self::parse_response_code($http_response_header),
'header' => implode("\r\n", $http_response_header), 'header' => implode("\r\n", $http_response_header),
'body' => $body, 'body' => $body,
'error' => $error ? $error['code'] : false, 'error' => $error ? $error['code'] : false,
'error_description' => $error ? $error['description'] : false, 'error_description' => $error ? $error['description'] : false,
'url' => $url, 'url' => $url,
);
];
} }
private function _stream_context($method, $url, $body=false, $headers=[]) { private function _stream_context($method, $url, $body=false, $headers=[]) {
@ -115,7 +115,7 @@ class Stream implements Transport {
$code = $match[1]; $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=[]) { public function head($url, $headers=[]) {
$response = $this->_read_file($url); $response = $this->_read_file($url);
return array(
'code' => $response['code'],
return [
'code' => (int)$response['code'],
'headers' => $response['headers'], 'headers' => $response['headers'],
'rels' => $response['rels'], 'rels' => $response['rels'],
'error' => '', 'error' => '',
'error_description' => '', 'error_description' => '',
'url' => $response['url'] 'url' => $response['url']
);
];
} }
private function _read_file($url) { private function _read_file($url) {
@ -100,19 +100,19 @@ class Test implements Transport {
$effectiveUrl = $url; $effectiveUrl = $url;
} }
return array(
'code' => $code,
return [
'code' => (int)$code,
'headers' => $parsedHeaders, 'headers' => $parsedHeaders,
'rels' => \IndieWeb\http_rels($headers), 'rels' => \IndieWeb\http_rels($headers),
'body' => $body, 'body' => $body,
'error' => (isset($parsedHeaders['X-Test-Error']) ? $parsedHeaders['X-Test-Error'] : ''), 'error' => (isset($parsedHeaders['X-Test-Error']) ? $parsedHeaders['X-Test-Error'] : ''),
'error_description' => '', 'error_description' => '',
'url' => $effectiveUrl 'url' => $effectiveUrl
);
];
} }
private static function _parse_headers($headers) { private static function _parse_headers($headers) {
$retVal = array();
$retVal = [];
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers)); $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
foreach($fields as $field) { foreach($fields as $field) {
if(preg_match('/([^:]+): (.+)/m', $field, $match)) { if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
@ -125,7 +125,7 @@ class Test implements Transport {
}, strtolower(trim($match[1]))); }, strtolower(trim($match[1])));
if(isset($retVal[$match[1]])) { if(isset($retVal[$match[1]])) {
if(!is_array($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]; $retVal[$match[1]][] = $match[2];
} else { } else {
$retVal[$match[1]] = trim($match[2]); $retVal[$match[1]] = trim($match[2]);

Loading…
Cancel
Save