Skip to content
Snippets Groups Projects
Commit 2cd05c2a authored by Marc Egger's avatar Marc Egger
Browse files

add alternative curl rest client, example works

parent a7984d17
No related branches found
No related tags found
2 merge requests!302Develop,!296Marc: Form/Report As File, Path class, Config class, Typo3 v9 compatability
Pipeline #3794 failed
......@@ -56,6 +56,9 @@ class RestClient {
$options['http']['content'] = $param[TOKEN_L_CONTENT];
}
$recvBuffer = self::callAPIMarc(strtoupper($param[TOKEN_L_METHOD]), $param[TOKEN_REST_CLIENT], $data = json_decode($param[TOKEN_L_CONTENT]));
/*
$context = stream_context_create($options);
try {
if (false === ($recvBuffer = file_get_contents($param[TOKEN_REST_CLIENT], false, $context))) {
......@@ -75,7 +78,7 @@ class RestClient {
// Copy new values to STORE_CLIENT
$this->store::setStore($recv, STORE_CLIENT, true);
*/
return $recvBuffer;
}
......@@ -134,4 +137,61 @@ class RestClient {
$param[TOKEN_L_HEADER] = KeyValueStringParser::unparse($header, ': ', '\r\n') . '\r\n';
return $param;
}
private static function callAPIMarc($method, $url, $data = array()) {
$ch = curl_init();
$curlConfig = array(
CURLOPT_RETURNTRANSFER => true,
// CURLINFO_HEADER_OUT => $debug
);
$curlHeader = array(
'Content-Type: application/json'
);
switch ($method) {
case "POST":
$curlConfig[CURLOPT_POST] = true;
if (!empty($data)) {
$dataJson = json_encode($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);
break;
case "DELETE":
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'DELETE';
if (!empty($data))
$url = sprintf("%s?%s", $url, http_build_query($data));
break;
default:
if (!empty($data))
$url = sprintf("%s?%s", $url, http_build_query($data));
}
$curlConfig[CURLOPT_URL] = $url;
curl_setopt_array($ch, $curlConfig);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);
// send request
$output = json_decode(curl_exec($ch), true);
if ($output === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode >= 300) {
throw new Exception(' Api error: ' . $url . ' HTTP code: ' . $httpCode . ' Message: ' . ($output['error'] ?? '') . ($output['message'] ?? ''), E_ERROR);
}
// finished
curl_close($ch);
return $output;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment