UrlRequests curl: move options set logic to main curl wrapper call

change the curlRequest call to options array and build the options array
there.
Remove any options check + pre build from the get/request calls

Update phpunit tests with string type body return
This commit is contained in:
Clemens Schwaighofer
2024-11-07 11:20:37 +09:00
parent 0c51a3be87
commit 8613e8977b
6 changed files with 151 additions and 79 deletions

View File

@@ -16,16 +16,26 @@ $log = new CoreLibs\Logging\Logging([
* build return json
*
* @param array<string,mixed> $http_headers
* @param string $body
* @param ?string $body
* @return string
*/
function buildContent(array $http_headers, string $body): string
function buildContent(array $http_headers, ?string $body): string
{
if (is_string($body) && !empty($body)) {
$_body = Json::jsonConvertToArray($body);
if (Json::jsonGetLastError()) {
$body = [$body];
} else {
$body = $_body;
}
} elseif (is_string($body)) {
$body = [];
}
return Json::jsonConvertArrayTo([
'HEADERS' => $http_headers,
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
"PARAMS" => $_GET,
"BODY" => Json::jsonConvertToArray($body),
"BODY" => $body,
// "STRING_BODY" => $body,
]);
}
@@ -45,7 +55,10 @@ if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RU
exit;
}
if (($file_get = file_get_contents('php://input')) === false) {
// if server request type is get set file_get to null -> no body
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$file_get = null;
} elseif (($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;

View File

@@ -121,6 +121,23 @@ $data = $client->request(
]
);
print "[request] _POST RESPONSE: <pre>" . print_r($data, true) . "</pre>";
print "<hr>";
$data = $client->request(
"post",
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
. '?other=post_a',
[
"body" => 'string body here',
"headers" => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'test-header' => 'ABC',
'info-request-type' => '_POST',
],
"query" => ['foo' => 'BAR post'],
]
);
print "[request|string body] _POST RESPONSE: <pre>" . print_r($data, true) . "</pre>";
print "<hr>";
$data = $client->put(