Compare commits

..

10 Commits

Author SHA1 Message Date
Clemens Schwaighofer
0c51a3be87 Add phpunit tests for header key/value exceptions 2024-11-06 18:49:48 +09:00
Clemens Schwaighofer
f9cf36524e UrlRequests auth set allowed in requests call
Removed the parseHeaders public call, headers must be set as array

Throw errors on invalid headers before sending them: Key/Value check
Add headers invalid check in phpunit

Auth headers can be set per call and will override global settings if matching
2024-11-06 18:42:35 +09:00
Clemens Schwaighofer
bacb9881ac Fix UrlRequests Interface name, fix header build
Header default build was not done well, pass original headers inside and
set them. On new default start with empty array.

Switch to CoreLibs Json calls, because we use this libarary anyway already
2024-11-06 14:28:15 +09:00
Clemens Schwaighofer
f0fae1f76d Fix Composer package phpunit test url for UrlRequests 2024-11-06 13:35:00 +09:00
Clemens Schwaighofer
1653e6b684 Allow http_errors unset/set on each call
If set or not set, on each call this option can be set.
If set to null on call, the original value or default config value is used
2024-11-06 13:29:19 +09:00
Clemens Schwaighofer
c8bc0062ad URL Requests change error response
Instead of just throwing exception on 401 auth, throw exception for any
error code from 400 on
This can be turned off with the option "http_errors" set to false

Also updaed the exception content to match 400 or 500 error type with
more information attached

General Exception error codes:
Cnnn: Curl errors (FAILURE)
Rnnn: general class errors (ERROR)
Hnnn: http response errors (ERROR)
2024-11-06 12:48:01 +09:00
Clemens Schwaighofer
5c8a2ef8da Update test paths for URLRequests tests 2024-11-06 10:38:30 +09:00
Clemens Schwaighofer
d8379a10d9 URL Request phpunit test added 2024-11-06 10:33:05 +09:00
Clemens Schwaighofer
30e2f33620 Test calls update for admin area 2024-11-06 10:03:33 +09:00
Clemens Schwaighofer
a4f16f4ca9 Various updates and fixes during testing
Move the build auth content to dedicated variables
Add a default User-Agent that is always sent
Default headers like Authorization and User-Agent are always set, even when
request is sent with headers null
Fix timeout, was sent as is and not converted to milliseconds
Fix headers not correctly set to null if array entry was set to null
2024-11-06 10:03:14 +09:00
7 changed files with 1684 additions and 275 deletions

View File

@@ -1,4 +1,4 @@
<?php
<?php // phpcs:ignore PSR1.Files.SideEffects
/**
* AUTHOR: Clemens Schwaighofer
@@ -9,20 +9,43 @@
declare(strict_types=1);
/**
* build return json
*
* @param array<string,mixed> $http_headers
* @param string $body
* @return string
*/
function buildContent(array $http_headers, string $body): string
{
return json_encode([
'HEADERS' => $http_headers,
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
"PARAMS" => $_GET,
"BODY" => json_decode($body, true)
]);
}
$http_headers = array_filter($_SERVER, function ($value, $key) {
if (str_starts_with($key, 'HTTP_')) {
return true;
}
}, ARRAY_FILTER_USE_BOTH);
$file_get = file_get_contents('php://input') ?: '["code": 500, "content": {"Error" => "file_get_contents failed"}]';
header("Content-Type: application/json; charset=UTF-8");
print json_encode([
'HEADERS' => $http_headers,
"PARAMS" => $_GET,
"BODY" => json_decode($file_get, true),
]);
// if the header has Authorization and RunAuthTest then exit with 401
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
header("HTTP/1.1 401 Unauthorized");
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
exit;
}
if (($file_get = file_get_contents('php://input')) === false) {
header("HTTP/1.1 404 Not Found");
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
exit;
}
print buildContent($http_headers, $file_get);
// __END__

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
<?php
<?php // phpcs:ignore PSR1.Files.SideEffects
declare(strict_types=1);
@@ -12,28 +12,54 @@ $log = new CoreLibs\Logging\Logging([
'log_per_date' => true,
]);
/**
* build return json
*
* @param array<string,mixed> $http_headers
* @param string $body
* @return string
*/
function buildContent(array $http_headers, string $body): string
{
return Json::jsonConvertArrayTo([
'HEADERS' => $http_headers,
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
"PARAMS" => $_GET,
"BODY" => Json::jsonConvertToArray($body),
// "STRING_BODY" => $body,
]);
}
$http_headers = array_filter($_SERVER, function ($value, $key) {
if (str_starts_with($key, 'HTTP_')) {
return true;
}
}, ARRAY_FILTER_USE_BOTH);
$file_get = file_get_contents('php://input') ?: '{"Error" => "file_get_contents failed"}';
header("Content-Type: application/json; charset=UTF-8");
// if the header has Authorization and RunAuthTest then exit with 401
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
header("HTTP/1.1 401 Unauthorized");
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
exit;
}
if (($file_get = file_get_contents('php://input')) === false) {
header("HTTP/1.1 404 Not Found");
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
exit;
}
// str_replace('\"', '"', trim($file_get, '"'));
$log->debug('SERVER', $log->prAr($_SERVER));
$log->debug('HEADERS', $log->prAr($http_headers));
$log->debug('REQUEST TYPE', $_SERVER['REQUEST_METHOD']);
$log->debug('GET', $log->prAr($_GET));
$log->debug('POST', $log->prAr($_POST));
$log->debug('PHP-INPUT', $log->prAr($file_get));
header("Content-Type: application/json; charset=UTF-8");
print Json::jsonConvertArrayTo([
'HEADERS' => $http_headers,
"PARAMS" => $_GET,
"BODY" => Json::jsonConvertToArray($file_get),
]);
print buildContent($http_headers, $file_get);
$log->debug('[END]', '=========================================>');

View File

@@ -40,11 +40,11 @@ $data = $client->get(
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
. '?other=get_a',
[
'headers' => $client->prepareHeaders([
'test-header: ABC',
'info-request-type: _GET',
'headers' => [
'test-header' => 'ABC',
'info-request-type' => '_GET',
'Funk-pop' => 'Semlly god'
]),
],
'query' => ['foo' => 'BAR']
]
);
@@ -78,11 +78,11 @@ $data = $client->request(
. 'trunk/www/admin/UrlRequests.target.php'
. '?other=get_a',
[
"headers" => $client->prepareHeaders([
'test-header: ABC',
'info-request-type: _GET',
"headers" => [
'test-header' => 'ABC',
'info-request-type' => '_GET',
'Funk-pop' => 'Semlly god'
]),
],
"query" => ['foo' => 'BAR'],
],
);
@@ -94,12 +94,12 @@ $data = $client->post(
. '?other=post_a',
[
'body' => ['payload' => 'data post'],
'headers' => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _POST'
]),
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_POST',
],
'query' => ['foo' => 'BAR post'],
]
);
@@ -111,12 +111,12 @@ $data = $client->request(
. '?other=post_a',
[
"body" => ['payload' => 'data post', 'request' => 'I am the request body'],
"headers" => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _POST'
]),
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_POST',
],
"query" => ['foo' => 'BAR post'],
]
);
@@ -128,12 +128,12 @@ $data = $client->put(
. '?other=put_a',
[
"body" => ['payload' => 'data put'],
"headers" => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _PUT'
]),
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_PUT',
],
'query' => ['foo' => 'BAR put'],
]
);
@@ -145,12 +145,12 @@ $data = $client->patch(
. '?other=patch_a',
[
"body" => ['payload' => 'data patch'],
"headers" => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _PATCH'
]),
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_PATCH',
],
'query' => ['foo' => 'BAR patch'],
]
);
@@ -162,12 +162,12 @@ $data = $client->delete(
. '?other=delete_no_body_a',
[
"body" => null,
"headers" => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _DELETE'
]),
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_DELETE',
],
"query" => ['foo' => 'BAR delete'],
]
);
@@ -179,12 +179,12 @@ $data = $client->delete(
. '?other=delete_body_a',
[
"body" => ['payload' => 'data delete'],
"headers" => $client->prepareHeaders([
'Content-Type: application/json',
'Accept: application/json',
'test-header: ABC',
'info-request-type: _DELETE'
]),
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_DELETE',
],
"query" => ['foo' => 'BAR delete'],
]
);
@@ -204,6 +204,9 @@ try {
'default-remove-array-part-alt' => ['c', 'd', 'e'],
'default-overwrite' => 'will be overwritten',
'default-add' => 'will be added',
],
'query' => [
'global-p' => 'glob'
]
]);
print "CONFIG: <pre>" . print_r($uc->getConfig(), true) . "</pre>";
@@ -217,13 +220,16 @@ try {
print "CONFIG: <pre>" . print_r($uc->getConfig(), true) . "</pre>";
$data = $uc->request(
'get',
'UrlRequests.target.php?other=get_a',
'UrlRequests.target.php',
[
'headers' => [
'call-header' => 'call-get',
'default-header' => 'overwrite-uc-get',
'X-Foo' => ['bar', 'baz'],
]
],
'query' => [
'other' => 'get_a',
],
]
);
print "[uc] _GET RESPONSE, nothing set: <pre>" . print_r($data, true) . "</pre>";
@@ -234,6 +240,104 @@ try {
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
}
print "<hr>";
try {
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"http_errors" => false,
"headers" => [
"Authorization" => "schmalztiegel",
"RunAuthTest" => "yes",
]
]);
$response = $uc->get('UrlRequests.target.php');
print "AUTH REQUEST: <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
} catch (Exception $e) {
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
}
print "AUTH REQUEST WITH EXCEPTION:<br>";
try {
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"http_errors" => true,
"headers" => [
"Authorization" => "schmalztiegel",
"RunAuthTest" => "yes",
]
]);
$response = $uc->get('UrlRequests.target.php');
print "AUTH REQUEST: <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
} catch (Exception $e) {
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
}
print "AUTH REQUEST WITH EXCEPTION (UNSET):<br>";
try {
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"http_errors" => true,
"headers" => [
"Authorization" => "schmalztiegel",
"RunAuthTest" => "yes",
]
]);
$response = $uc->get('UrlRequests.target.php', ['http_errors' => false]);
print "AUTH REQUEST (UNSET): <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
} catch (Exception $e) {
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
}
print "AUTH REQUEST HEADER SET:<br>";
try {
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"auth" => ["user", "pass", "basic"],
"headers" => [
"Authorization" => "schmalztiegel",
"RunAuthTest" => "yes",
]
]);
$response = $uc->get('UrlRequests.target.php');
print "AUTH REQUEST (HEADER): <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
} catch (Exception $e) {
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
}
print "<hr>";
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"headers" => [
"header-one" => "one"
]
]);
$response = $uc->get('UrlRequests.target.php', ["headers" => null, "query" => ["test" => "one-test"]]);
print "HEADER RESET REQUEST: <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
print "<hr>";
$uc = new Curl([
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
"headers" => [
'bar' => 'foo:bar'
]
]);
$response = $uc->get('UrlRequests.target.php');
print "HEADER SET TEST REQUEST: <pre>" . print_r($response, true) . "</pre>";
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
print "</body></html>";

View File

@@ -10,7 +10,7 @@
* https://docs.guzzlephp.org/en/stable/index.html
*
* Requests are guzzleHttp compatible
* Config for setup is guzzleHttp compatible (except the exception_on_not_authorized)
* Config for setup is guzzleHttp compatible (except the http_errors)
* Any setters and getters are only for this class
*/
@@ -18,10 +18,8 @@ declare(strict_types=1);
namespace CoreLibs\UrlRequests;
use RuntimeException;
use CoreLibs\Convert\Json;
/** @package CoreLibs\UrlRequests */
class Curl implements Interface\RequestsInterface
{
// all general calls: get/post/put/patch/delete
@@ -35,6 +33,12 @@ class Curl implements Interface\RequestsInterface
private const HAVE_POST_FIELDS = ["post", "put", "patch", "delete"];
/** @var array<string> list of requests that must have a body */
private const MANDATORY_POST_FIELDS = ["post", "put", "patch"];
/** @var int http ok request */
public const HTTP_OK = 200;
/** @var int http ok creted response */
public const HTTP_CREATED = 201;
/** @var int http ok no content */
public const HTTP_NO_CONTENT = 204;
/** @var int error bad request */
public const HTTP_BAD_REQUEST = 400;
/** @var int error not authorized Request */
@@ -47,27 +51,22 @@ class Curl implements Interface\RequestsInterface
public const HTTP_CONFLICT = 409;
/** @var int error unprocessable entity */
public const HTTP_UNPROCESSABLE_ENTITY = 422;
/** @var int http ok request */
public const HTTP_OK = 200;
/** @var int http ok creted response */
public const HTTP_CREATED = 201;
/** @var int http ok no content */
public const HTTP_NO_CONTENT = 204;
/** @var int major version for user agent */
public const MAJOR_VERSION = 1;
// the config is set to be as much compatible to guzzelHttp as possible
// phpcs:disable Generic.Files.LineLength
/** @var array{auth?:array{0:string,1:string,2:string},auth_type?:int|string,auth_userpwd?:string,exception_on_not_authorized:bool,base_uri:string,headers:array<string,string|array<string>>,query:array<string,string>,timeout:float,connection_timeout:float} config settings as
/** @var array{auth?:array{0:string,1:string,2:string},http_errors:bool,base_uri:string,headers:array<string,string|array<string>>,query:array<string,string>,timeout:float,connection_timeout:float} config settings as
*phpcs:enable Generic.Files.LineLength
* auth: [0: user, 1: password, 2: auth type]
* http_errors: default true, bool true/false for throwing exception on >= 400 HTTP errors
* base_uri: base url to set, will prefix all urls given in calls
* headers: (array) base headers, can be overwritten by headers set in call
* timeout: default 0, in seconds (CURLOPT_TIMEOUT_MS)
* connect_timeout: default 300, in seconds (CURLOPT_CONNECTTIMEOUT_MS)
* : below is not a guzzleHttp config
* exception_on_not_authorized: bool true/false for throwing exception on auth error
*/
private array $config = [
'exception_on_not_authorized' => false,
'http_errors' => true,
'base_uri' => '',
'query' => [],
'headers' => [],
@@ -78,6 +77,12 @@ class Curl implements Interface\RequestsInterface
private array $parsed_base_uri = [];
/** @var array<string,string> lower key header name matches to given header name */
private array $headers_named = [];
/** @var int auth type from auth array in config */
private int $auth_type = 0;
/** @var string username and password string from auth array in config */
private string $auth_userpwd = '';
/** @var string set if auth type basic is given, will be set as "Authorization: ..." */
private string $auth_basic_header = '';
/** @var array<string,array<string>> received headers per header name, with sub array if there are redirects */
private array $received_headers = [];
@@ -107,14 +112,14 @@ class Curl implements Interface\RequestsInterface
* Set the main configuration
*
* phpcs:disable Generic.Files.LineLength
* @param array{auth?:array{0:string,1:string,2:string},auth_type?:int|string,auth_userpwd?:string,exception_on_not_authorized?:bool,base_uri?:string,headers?:array<string,string|array<string>>,query?:array<string,string>,timeout?:float,connection_timeout?:float} $config
* @param array{auth?:array{0:string,1:string,2:string},http_errors?:bool,base_uri?:string,headers?:array<string,string|array<string>>,query?:array<string,string>,timeout?:float,connection_timeout?:float} $config
* @return void
* phpcs:enable Generic.Files.LineLength
*/
private function setConfiguration(array $config)
{
$default_config = [
'exception_on_not_authorized' => false,
'http_errors' => true,
'base_uri' => '',
'query' => [],
'headers' => [],
@@ -123,37 +128,19 @@ class Curl implements Interface\RequestsInterface
];
// auth string is array of 0: user, 1: password, 2: auth type
if (!empty($config['auth']) && is_array($config['auth'])) {
// base auth sets the header actually
$type = isset($config['auth'][2]) ? strtolower($config['auth'][2]) : 'basic';
$userpwd = $config['auth'][0] . ':' . $config['auth'][1];
switch ($type) {
case 'basic':
if (!isset($config['headers']['Authorization'])) {
$config['headers']['Authorization'] = 'Basic ' . base64_encode(
$userpwd
);
}
break;
case 'digest':
$config['auth_type'] = CURLAUTH_DIGEST;
$config['auth_userpwd'] = $userpwd;
break;
case 'ntlm':
$config['auth_type'] = CURLAUTH_NTLM;
$config['auth_userpwd'] = $userpwd;
break;
}
$auth_data = $this->authParser($config['auth']);
$this->auth_basic_header = $auth_data['auth_basic_header'];
$this->auth_type = $auth_data['auth_type'];
$this->auth_userpwd = $auth_data['auth_userpwd'];
}
// only set if bool
if (
!isset($config['exception_on_not_authorized']) ||
!is_bool($config['exception_on_not_authorized'])
!isset($config['http_errors']) ||
!is_bool($config['http_errors'])
) {
$config['exception_on_not_authorized'] = false;
$config['http_errors'] = true;
}
if (!empty($config['base_uri'])) {
// TODO: this should be run through Guzzle\Psr7\Utils in future
// $config['base_uri'] = Psr7\Utils::urlFor($config['base_uri']);
if (($parsed_base_uri = $this->parseUrl($config['base_uri'])) !== false) {
$this->parsed_base_uri = $parsed_base_uri;
$config['base_uri'] = $config['base_uri'];
@@ -180,6 +167,46 @@ class Curl implements Interface\RequestsInterface
$this->config = array_merge($default_config, $config);
}
// MARK: auth parser
/**
* set various auth parameters and return them as array for further processing
*
* @param array{0:string,1:string,2:string} $auth
* @return array{auth_basic_header:string,auth_type:int,auth_userpwd:string}
*/
private function authParser(?array $auth): array
{
$return_auth = [
'auth_basic_header' => '',
'auth_type' => 0,
'auth_userpwd' => '',
];
// on empty return as is, to force defaults
if ($auth === []) {
return $return_auth;
}
// base auth sets the header actually
$type = isset($auth[2]) ? strtolower($auth[2]) : 'basic';
$userpwd = $auth[0] . ':' . $auth[1];
switch ($type) {
case 'basic':
$return_auth['auth_basic_header'] = 'Basic ' . base64_encode(
$userpwd
);
break;
case 'digest':
$return_auth['auth_type'] = CURLAUTH_DIGEST;
$return_auth['auth_userpwd'] = $userpwd;
break;
case 'ntlm':
$return_auth['auth_type'] = CURLAUTH_NTLM;
$return_auth['auth_userpwd'] = $userpwd;
break;
}
return $return_auth;
}
// MARK: parse and build url
/**
@@ -303,6 +330,14 @@ class Curl implements Interface\RequestsInterface
$this->parsed_url = $parsed_url;
}
}
// build query with global query
// any query set in the base_url or url_req will be overwritten
if (!empty($this->config['query'])) {
// add current query if set
// for params: if foo[0] then we ADD as php array type
// note that this has to be done on the user side, we just merge and local overrides global
$query = array_merge($this->config['query'], $query ?? []);
}
if (is_array($query)) {
$query = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
@@ -356,6 +391,7 @@ class Curl implements Interface\RequestsInterface
private function convertHeaders(array $headers): array
{
$return_headers = [];
$header_keys = [];
foreach ($headers as $key => $value) {
if (!is_string($key)) {
// TODO: throw error
@@ -363,8 +399,19 @@ class Curl implements Interface\RequestsInterface
}
// bad if not valid header key
if (!preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $key)) {
// TODO throw error
continue;
throw new \UnexpectedValueException(
Json::jsonConvertArrayTo([
'status' => 'ERROR',
'code' => 'R002',
'type' => 'InvalidHeaderKey',
'message' => 'Header key contains invalid characters',
'context' => [
'key' => $key,
'allowed' => '/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D',
],
]),
1
);
}
// if value is array, join to string
if (is_array($value)) {
@@ -373,8 +420,20 @@ class Curl implements Interface\RequestsInterface
$value = trim((string)$value, " \t");
// header values must be valid
if (!preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D', $value)) {
// TODO throw error
continue;
throw new \UnexpectedValueException(
Json::jsonConvertArrayTo([
'status' => 'ERROR',
'code' => 'R003',
'type' => 'InvalidHeaderValue',
'message' => 'Header value contains invalid characters',
'context' => [
'key' => $key,
'value' => $value,
'allowed' => '/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D',
],
]),
1
);
}
$return_headers[] = (string)$key . ':' . $value;
}
@@ -382,17 +441,49 @@ class Curl implements Interface\RequestsInterface
return $return_headers;
}
/**
* default headers that are always set
* Authorization
* User-Agent
*
* @param array<string,string|array<string>> $headers already set headers
* @param ?string $auth_basic_header
* @return array<string,string|array<string>>
*/
private function buildDefaultHeaders(array $headers = [], ?string $auth_basic_header = ''): array
{
// add auth header if set, will overwrite any already set auth header
if ($auth_basic_header !== null && $auth_basic_header == '' && !empty($this->auth_basic_header)) {
$auth_basic_header = $this->auth_basic_header;
}
if (!empty($auth_basic_header)) {
// check if there is any auth header set, remove that one
if (!empty($auth_header_set = $this->headers_named[strtolower('Authorization')] ?? null)) {
unset($headers[$auth_header_set]);
}
// set new auth header
$headers['Authorization'] = $auth_basic_header;
}
// always add HTTP_HOST and HTTP_USER_AGENT
if (!isset($headers[strtolower('User-Agent')])) {
$headers['User-Agent'] = 'CoreLibsUrlRequestCurl/' . self::MAJOR_VERSION;
}
return $headers;
}
/**
* Build headers, combine with global headers of they are set
*
* @param null|array<string,string|array<string>> $headers
* @param ?string $auth_basic_header
* @return array<string,string|array<string>>
*/
private function buildHeaders(null|array $headers): array
private function buildHeaders(null|array $headers, ?string $auth_basic_header): array
{
// if headers is null, return empty headers, do not set default headers
// if headers is null, return empty headers, do not set config default headers
// but the automatic set User-Agent and Authorization headers are always set
if ($headers === null) {
return [];
return $this->buildDefaultHeaders(auth_basic_header: $auth_basic_header);
}
// merge master headers with sub headers, sub headers overwrite master headers
if (!empty($this->config['headers'])) {
@@ -410,7 +501,7 @@ class Curl implements Interface\RequestsInterface
$headers[$key] = $this->config['headers'][$key];
}
}
// always add HTTP_HOST and HTTP_USER_AGENT
$headers = $this->buildDefaultHeaders($headers, $auth_basic_header);
return $headers;
}
@@ -419,29 +510,49 @@ class Curl implements Interface\RequestsInterface
/**
* Overall request call
*
* @param string $type get, post, pathc, put, delete:
* if not set or invalid throw error
* @param string $url The URL being requested,
* including domain and protocol
* @param null|array<string,string|array<string>> $headers [default=[]] Headers to be used in the request
* @param null|array<string,string> $query [default=null] Optinal query parameters
* @param null|string|array<string,mixed> $body [default=null] Data body, converted to JSON
* @param string $type get, post, pathc, put, delete:
* if not set or invalid throw error
* @param string $url The URL being requested,
* including domain and protocol
* @param null|array<string,string|array<string>> $headers Headers to be used in the request
* @param null|array<string,string> $query Optinal query parameters
* @param null|string|array<string,mixed> $body Data body, converted to JSON
* @param null|bool $http_errors Throw exception on http response
* 400 or higher if set to true
* @param null|array{0:string,1:string,2:string} $auth auth array, if null reset global set auth
* @return array{code:string,headers:array<string,array<string>>,content:string}
* @throws \RuntimeException if type param is not valid
*/
private function curlRequest(
string $type,
string $url,
null|array $headers = [],
null|array $query = null,
null|string|array $body = null
null|array $headers,
null|array $query,
null|string|array $body,
null|bool $http_errors,
null|array $auth,
): array {
// set auth from override
if (is_array($auth)) {
$auth_data = $this->authParser($auth);
} else {
$auth_data = [
'auth_basic_header' => null,
'auth_type' => null,
'auth_userpwd' => null,
];
}
// build url
$this->url = $this->buildQuery($url, $query);
$this->headers = $this->convertHeaders($this->buildHeaders($headers));
$this->headers = $this->convertHeaders($this->buildHeaders(
$headers,
$auth_data['auth_basic_header']
));
if (!in_array($type, self::VALID_REQUEST_TYPES)) {
throw new RuntimeException(
json_encode([
'status' => 'FAILURE',
'code' => 'C003',
throw new \RuntimeException(
Json::jsonConvertArrayTo([
'status' => 'ERROR',
'code' => 'R001',
'type' => 'InvalidRequestType',
'message' => 'Invalid request type set: ' . $type,
'context' => [
@@ -449,14 +560,17 @@ class Curl implements Interface\RequestsInterface
'url' => $this->url,
'headers' => $this->headers,
],
]) ?: '',
]),
0,
);
}
// init curl handle
$handle = $this->handleCurleInit($this->url);
// set the standard curl options
$this->setCurlOptions($handle, $this->headers);
$this->setCurlOptions($handle, $this->headers, [
'auth_type' => $auth_data['auth_type'],
'auth_userpwd' => $auth_data['auth_userpwd'],
]);
// for post we set POST option
if ($type == "post") {
curl_setopt($handle, CURLOPT_POST, true);
@@ -474,7 +588,7 @@ class Curl implements Interface\RequestsInterface
// for debug
// print "CURLINFO_HEADER_OUT: <pre>" . curl_getinfo($handle, CURLINFO_HEADER_OUT) . "</pre>";
// get response code and bail on not authorized
$http_response = $this->handleCurlResponse($http_result, $handle);
$http_response = $this->handleCurlResponse($handle, $http_result, $http_errors);
// close handler
$this->handleCurlClose($handle);
// return response and result
@@ -492,6 +606,7 @@ class Curl implements Interface\RequestsInterface
*
* @param string $url
* @return \CurlHandle
* @throws \RuntimeException if curl could not be initialized
*/
private function handleCurleInit(string $url): \CurlHandle
{
@@ -500,8 +615,8 @@ class Curl implements Interface\RequestsInterface
return $handle;
}
// throw Error here with all codes
throw new RuntimeException(
json_encode([
throw new \RuntimeException(
Json::jsonConvertArrayTo([
'status' => 'FAILURE',
'code' => 'C001',
'type' => 'CurlInitError',
@@ -509,7 +624,7 @@ class Curl implements Interface\RequestsInterface
'context' => [
'url' => $url,
],
]) ?: '',
]),
0,
);
}
@@ -523,19 +638,32 @@ class Curl implements Interface\RequestsInterface
*
* @param \CurlHandle $handle
* @param array<string> $headers list of options
* @param array{auth_type:?int,auth_userpwd:?string} $auth_data auth options to override global
* @return void
*/
private function setCurlOptions(\CurlHandle $handle, array $headers): void
private function setCurlOptions(\CurlHandle $handle, array $headers, ?array $auth_data): void
{
// for not Basic auth, basic auth sets its own header
if (!empty($this->config['auth_type']) && !empty($this->config['auth_userpwd'])) {
curl_setopt($handle, CURLOPT_HTTPAUTH, $this->config['auth_type']);
curl_setopt($handle, CURLOPT_USERPWD, $this->config['auth_userpwd']);
// for not Basic auth only, basic auth sets its own header
if ($auth_data['auth_type'] !== null || $auth_data['auth_userpwd'] !== null) {
// set global if any of the two is empty and both globals are set
if (
(empty($auth_data['auth_type']) || empty($auth_data['auth_userpwd'])) &&
!empty($this->auth_type) && !empty($this->auth_userpwd)
) {
$auth_data['auth_type'] = $this->auth_type;
$auth_data['auth_userpwd'] = $this->auth_userpwd;
}
}
// set auth options for curl
if (!empty($auth_data['auth_type']) && !empty($auth_data['auth_userpwd'])) {
curl_setopt($handle, CURLOPT_HTTPAUTH, $auth_data['auth_type']);
curl_setopt($handle, CURLOPT_USERPWD, $auth_data['auth_userpwd']);
}
if ($headers !== []) {
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
}
// curl_setopt($handle, CURLOPT_FAILONERROR, true);
// return response as string and not just HTTP_OK
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
// for debug only
curl_setopt($handle, CURLINFO_HEADER_OUT, true);
@@ -546,12 +674,14 @@ class Curl implements Interface\RequestsInterface
$timeout_requires_no_signal = false;
// if we have a timeout signal
if (!empty($this->config['timeout'])) {
$timeout_requires_no_signal |= $this->config['timeout'] < 1;
curl_setopt($handle, CURLOPT_TIMEOUT_MS, $this->config['timeout']);
$timeout_requires_no_signal = $timeout_requires_no_signal ||
$this->config['timeout'] < 1;
curl_setopt($handle, CURLOPT_TIMEOUT_MS, $this->config['timeout'] * 1000);
}
if (!empty($this->config['connection_timeout'])) {
$timeout_requires_no_signal |= $this->config['connection_timeout'] < 1;
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT_MS, $this->config['connection_timeout']);
$timeout_requires_no_signal = $timeout_requires_no_signal ||
$this->config['connection_timeout'] < 1;
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT_MS, $this->config['connection_timeout'] * 1000);
}
if ($timeout_requires_no_signal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
curl_setopt($handle, CURLOPT_NOSIGNAL, true);
@@ -583,14 +713,17 @@ class Curl implements Interface\RequestsInterface
/**
* handles any CURL execute and on error throws a correct error message
*
* @param \CurlHandle $handle
* @return string
* @param \CurlHandle $handle Curl handler
* @return string Return content as string, if False will throw exception
* will only return HTTP_OK if CURLOPT_RETURNTRANSFER is turned off
* @throws \RuntimeException if the connection had an error
*/
private function handleCurlExec(\CurlHandle $handle): string
{
// execute query
$http_result = curl_exec($handle);
if ($http_result === true) {
// only if CURLOPT_RETURNTRANSFER is turned off
return (string)self::HTTP_OK;
} elseif ($http_result !== false) {
return $http_result;
@@ -617,18 +750,18 @@ class Curl implements Interface\RequestsInterface
}
// throw an error like in the normal reqeust, but set to CURL error
throw new RuntimeException(
json_encode([
throw new \RuntimeException(
Json::jsonConvertArrayTo([
'status' => 'FAILURE',
'code' => 'C002',
'type' => 'CurlError',
'type' => 'CurlExecError',
'message' => $message,
'context' => [
'url' => $url,
'errno' => $errno,
'message' => $message,
],
]) ?: '',
]),
$errno
);
}
@@ -636,45 +769,46 @@ class Curl implements Interface\RequestsInterface
// MARK: curl response handler
/**
* Handle curl response and not auth 401 errors
* Handle curl response, will throw exception on anything that is lower 400
* can be turned off by setting http_errors to false
*
* @param string $http_result
* @param \CurlHandle $handle
* @return string
* @param string $http_result result string from the url call
* @param ?bool $http_errors if we should throw an exception on error, override config setting
* @param \CurlHandle $handle Curl handler
* @return string http response code
* @throws \RuntimeException if http_errors is true then will throw exception on any response code >= 400
*/
private function handleCurlResponse(
\CurlHandle $handle,
string $http_result,
\CurlHandle $handle
?bool $http_errors
): string {
$http_response = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
if (
empty($this->config['exception_on_not_authorized']) ||
$http_response !== self::HTTP_NOT_AUTHORIZED
empty($http_errors ?? $this->config['http_errors']) ||
$http_response < self::HTTP_BAD_REQUEST
) {
return (string)$http_response;
}
// set curl error number
$err = curl_errno($handle);
// extract all the error codes
$result_ar = json_decode((string)$http_result, true);
$url = curl_getinfo($handle, CURLINFO_EFFECTIVE_URL);
$error_status = 'ERROR';
$error_code = $http_response;
$error_type = 'UnauthorizedRequest';
$message = 'Request could not be finished successfully because of an authorization error';
// throw Error here with all codes
throw new RuntimeException(
json_encode([
'status' => $error_status,
'code' => $error_code,
'type' => $error_type,
'message' => $message,
throw new \RuntimeException(
Json::jsonConvertArrayTo([
'status' => 'ERROR',
'code' => 'H' . (string)$http_response,
'type' => $http_response < 500 ? 'ClientError' : 'ServerError',
'message' => 'Request could not be finished successfully because of bad request response',
'context' => [
'url' => $url,
'result' => $result_ar,
'http_response' => $http_response,
// extract all the error content if returned
'result' => Json::jsonConvertToArray($http_result),
// curl internal error number
'curl_errno' => $err,
// the full curl info block
'curl_info' => curl_getinfo($handle),
],
]) ?: '',
]),
$err
);
}
@@ -694,48 +828,6 @@ class Curl implements Interface\RequestsInterface
// MARK: PUBLIC METHODS
// *********************************************************************
/**
* Convert an array with header strings like "foo: bar" to the interface
* needed "foo" => "bar" type
* Skips entries that are already in key => value type, by checking if the
* key is a not a number
*
* @param array<int|string,string> $headers
* @return array<string,string>
* @throws \UnexpectedValueException on duplicate header key
*/
public function prepareHeaders(array $headers): array
{
$return_headers = [];
foreach ($headers as $header_key => $header) {
// skip if header key is not numeric
if (!is_numeric($header_key)) {
$return_headers[$header_key] = $header;
continue;
}
list($_key, $_value) = explode(':', $header);
if (array_key_exists($_key, $return_headers)) {
// raise exception if key already exists
throw new \UnexpectedValueException(
json_encode([
'status' => 'ERROR',
'code' => 'C004',
'type' => 'DuplicatedArrayKey',
'message' => 'Key already exists in the headers',
'context' => [
'key' => $_key,
'headers' => $headers,
'return_headers' => $return_headers,
],
]) ?: '',
1
);
}
$return_headers[$_key] = $_value;
}
return $return_headers;
}
// MARK: get class vars
/**
@@ -823,7 +915,7 @@ class Curl implements Interface\RequestsInterface
* remove header entry
* if key is only set then match only key, if both are set both sides must match
*
* @param array<string,string> $remove_headers
* @param array<string,null|string|array<string>> $remove_headers
* @return void
*/
public function removeHeaders(array $remove_headers): void
@@ -861,10 +953,11 @@ class Curl implements Interface\RequestsInterface
if (!is_array($value)) {
$value = [$value];
}
$this->config['headers'][$header_key] = array_diff(
// array values so we rewrite the key pos
$this->config['headers'][$header_key] = array_values(array_diff(
$this->config['headers'][$header_key],
$value
);
));
}
}
}
@@ -897,7 +990,7 @@ class Curl implements Interface\RequestsInterface
* phpcs:disable Generic.Files.LineLength
* @param string $type
* @param string $url
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<string,mixed>} $options
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
* @throws \UnexpectedValueException on missing body data when body data is needed
* phpcs:enable Generic.Files.LineLength
@@ -917,9 +1010,11 @@ class Curl implements Interface\RequestsInterface
return $this->curlRequest(
$type,
$url,
$options['headers'] ?? [],
!array_key_exists('headers', $options) ? [] : $options['headers'],
$options['query'] ?? null,
$options['body'] ?? null
$options['body'] ?? null,
!array_key_exists('http_errors', $options) ? null : $options['http_errors'],
!array_key_exists('auth', $options) ? [] : $options['auth'],
);
}
}

View File

@@ -18,6 +18,30 @@ namespace CoreLibs\UrlRequests;
trait CurlTrait
{
/**
* Set the array block that is sent to the request call
* Make sure that if headers is set as key but null it stays null and set to empty array
* if headers key is missing
* "get" calls do not set any body
*
* @param string $type if set as get do not add body, else add body
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Request options
* @return array{auth?:array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool}
*/
private function setOptions(string $type, array $options): array
{
$base = [
"auth" => !array_key_exists('auth', $options) ? [] : $options['auth'],
"headers" => !array_key_exists('headers', $options) ? [] : $options['headers'],
"query" => $options['query'] ?? null,
"http_errors" => !array_key_exists('http_errors', $options) ? null : $options['http_errors'],
];
if ($type != "get") {
$base["body"] = $options['body'] ?? null;
}
return $base;
}
/**
* combined set call for any type of request with options type parameters
* The following options can be set:
@@ -27,11 +51,11 @@ trait CurlTrait
*
* @param string $type What type of request we send, will throw exception if not a valid one
* @param string $url The url to send
* @param array{headers?:null|array<string,string|array<string>>,query?:null|string|array<string,mixed>,body?:null|string|array<string,mixed>} $options Request options
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Request options
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
* @throws \UnexpectedValueException on missing body data when body data is needed
*/
abstract public function request(string $type, string $url, array $options): array;
abstract public function request(string $type, string $url, array $options = []): array;
/**
* Makes an request to the target url via curl: GET
@@ -39,18 +63,15 @@ trait CurlTrait
*
* @param string $url The URL being requested,
* including domain and protocol
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
*/
public function get(string $url, array $options): array
public function get(string $url, array $options = []): array
{
return $this->request(
"get",
$url,
[
"headers" => $options['headers'] ?? [],
"query" => $options['query'] ?? null,
],
$this->setOptions('get', $options),
);
}
@@ -60,7 +81,7 @@ trait CurlTrait
*
* @param string $url The URL being requested,
* including domain and protocol
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
*/
public function post(string $url, array $options): array
@@ -68,11 +89,7 @@ trait CurlTrait
return $this->request(
"post",
$url,
[
"headers" => $options['headers'] ?? [],
"query" => $options['query'] ?? null,
"body" => $options['body'] ?? null,
],
$this->setOptions('post', $options),
);
}
@@ -82,7 +99,7 @@ trait CurlTrait
*
* @param string $url The URL being requested,
* including domain and protocol
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
*/
public function put(string $url, array $options): array
@@ -90,11 +107,7 @@ trait CurlTrait
return $this->request(
"put",
$url,
[
"headers" => $options['headers'] ?? [],
"query" => $options['query'] ?? null,
"body" => $options['body'] ?? null,
],
$this->setOptions('put', $options),
);
}
@@ -104,7 +117,7 @@ trait CurlTrait
*
* @param string $url The URL being requested,
* including domain and protocol
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
*/
public function patch(string $url, array $options): array
@@ -112,11 +125,7 @@ trait CurlTrait
return $this->request(
"patch",
$url,
[
"headers" => $options['headers'] ?? [],
"query" => $options['query'] ?? null,
"body" => $options['body'] ?? null,
],
$this->setOptions('patch', $options),
);
}
@@ -127,19 +136,15 @@ trait CurlTrait
*
* @param string $url The URL being requested,
* including domain and protocol
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
*/
public function delete(string $url, array $options): array
public function delete(string $url, array $options = []): array
{
return $this->request(
"delete",
$url,
[
"headers" => $options['headers'] ?? [],
"query" => $options['query'] ?? null,
"body" => $options['body'] ?? null,
],
$this->setOptions('delete', $options),
);
}
}

View File

@@ -11,18 +11,6 @@ namespace CoreLibs\UrlRequests\Interface;
interface RequestsInterface
{
/**
* Convert an array with header strings like "foo: bar" to the interface
* needed "foo" => "bar" type
* Skips entries that are already in key => value type, by checking if the
* key is a not a number
*
* @param array<int|string,string> $headers
* @return array<string,string>
* @throws \UnexpectedValueException on duplicate header key
*/
public function prepareHeaders(array $headers): array;
/**
* get the config array with all settings
*
@@ -84,7 +72,7 @@ interface RequestsInterface
* phpcs:disable Generic.Files.LineLength
* @param string $type
* @param string $url
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<string,mixed>} $options
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
* @throws \UnexpectedValueException on missing body data when body data is needed
* phpcs:enable Generic.Files.LineLength