Commit a5a2c98a authored by Marc Egger's avatar Marc Egger
Browse files

bug fix gebastelt mit philipp

parent 28c16e84
Pipeline #3801 failed with stages
in 2 minutes and 1 second
......@@ -11,6 +11,7 @@
"ext-fileinfo": "*",
"ext-ldap": "*",
"ext-intl": "*",
"ext-iconv": "*"
"ext-iconv": "*",
"ext-curl": "*"
}
}
\ No newline at end of file
......@@ -39,16 +39,28 @@ class RestClient {
$recv = array();
$param = $this->parseArgument($str);
// split header at "\r\n" and ";". $header must be an array of single headers!!
if (!empty($param[TOKEN_L_HEADER])) {
$header = str_replace("\r\n", ';', $param[TOKEN_L_HEADER]);
$header = str_replace("\n", ';', $header);
$header = explode(';', $header);
} else {
$header = [];
}
$options = array(
'http' => array(
'header' => $param[TOKEN_L_HEADER],
'header' => $header,
'method' => strtoupper($param[TOKEN_L_METHOD]),
'timeout' => $param[TOKEN_L_TIMEOUT],
)
);
// TODO: ssl for CURL?
if (isset($param[TOKEN_L_SSL])) {
$options['ssl'] = json_decode($param[TOKEN_L_SSL]);
$options['ssl'] = json_decode($param[TOKEN_L_SSL], true);
}
// Add content only if there is one.
......@@ -56,11 +68,16 @@ class RestClient {
$options['http']['content'] = $param[TOKEN_L_CONTENT];
}
// Marc Curl Version
try {
list($recv[HTTP_STATUS], $recvBuffer) = self::callAPIMarc(strtoupper($param[TOKEN_L_METHOD]),
$param[TOKEN_REST_CLIENT],
$data = json_decode($param[TOKEN_L_CONTENT]));
list($http_status, $recvBuffer) = self::callAPIMarc(strtoupper($param[TOKEN_L_METHOD]), $param[TOKEN_REST_CLIENT], $data = $param[TOKEN_L_CONTENT], $header);
$recv['rawAnswer'] = $recvBuffer;
$rcvBufferDecoded = json_decode($recvBuffer, true);
array_walk_recursive($rcvBufferDecoded, function ($value, $key) use (&$recv) {
$recv[$key] = $value;
});
$recv[HTTP_STATUS] = $http_status;
} catch (\Exception $e) {
$recv[HTTP_STATUS] = $e->getCode();
$recv[ERROR_MESSAGE] = $e->getMessage();
......@@ -88,6 +105,7 @@ class RestClient {
*/
// Copy new values to STORE_CLIENT
$this->store::setStore($recv, STORE_CLIENT, true);
......@@ -146,44 +164,53 @@ class RestClient {
}
// Join all header arguments to one string
$param[TOKEN_L_HEADER] = KeyValueStringParser::unparse($header, ': ', '\r\n') . '\r\n';
// $param[TOKEN_L_HEADER] = KeyValueStringParser::unparse($header, ': ', '\r\n') . '\r\n';
return $param;
}
private static function callAPIMarc($method, $url, $data = array()) {
private static function callAPIMarc(string $method, string $url, string $data = '', array $header = []) {
$ch = curl_init();
$curlConfig = array(
CURLOPT_RETURNTRANSFER => true,
// CURLINFO_HEADER_OUT => $debug
);
$curlHeader = array(
'Content-Type: application/json'
);
if (!empty($header)) {
$curlHeader = $header;
} else {
$curlHeader = array(
'Content-Type: application/json'
);
}
switch ($method) {
case "POST":
$curlConfig[CURLOPT_POST] = true;
if (!empty($data)) {
$dataJson = json_encode($data);
$dataJson = $data;
$curlConfig[CURLOPT_POSTFIELDS] = $dataJson;
$curlHeader[] = 'Content-Length: ' . strlen($dataJson);
}
break;
case "PUT":
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'PUT';
if (!empty($data))
$dataJson = json_encode($data);
$curlConfig[CURLOPT_POSTFIELDS] = $dataJson;
$curlHeader[] = 'Content-Length: ' . strlen($dataJson);
if (!empty($data)) {
$dataJson = $data;
$curlConfig[CURLOPT_POSTFIELDS] = $dataJson;
$curlHeader[] = 'Content-Length: ' . strlen($dataJson);
}
break;
case "DELETE":
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'DELETE';
if (!empty($data))
$url = sprintf("%s?%s", $url, http_build_query($data));
if (!empty($data)) {
$url = sprintf("%s?%s", $url, http_build_query(json_decode($data, true)));
}
break;
default:
if (!empty($data))
$url = sprintf("%s?%s", $url, http_build_query($data));
if (!empty($data)) {
$url = sprintf("%s?%s", $url, http_build_query(json_decode($data, true)));
}
}
$curlConfig[CURLOPT_URL] = $url;
curl_setopt_array($ch, $curlConfig);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment