Files
ms1inscription-v5/php/chronotrack_api/fx_chronotrack_http.php

112 lines
3.7 KiB
PHP

<?php
/**
* MSIN API ChronoTrack — client HTTP bas niveau (cURL).
*/
function fxChronotrackApiHttpRequest($strMethod, $strUrl, $arrOptions = array()) {
$arrResult = array(
'state' => 'error',
'http_code' => 0,
'body' => '',
'json' => null,
'curl_error' => '',
);
if (!function_exists('curl_init')) {
$arrResult['curl_error'] = 'cURL indisponible';
return $arrResult;
}
$ch = curl_init();
$arrHeaders = isset($arrOptions['headers']) && is_array($arrOptions['headers']) ? $arrOptions['headers'] : array();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeaders);
$strMethod = strtoupper(trim($strMethod));
if ($strMethod === 'GET') {
curl_setopt($ch, CURLOPT_HTTPGET, true);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $strMethod);
if (isset($arrOptions['body'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrOptions['body']);
} elseif ($strMethod === 'POST') {
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
}
}
if (!empty($arrOptions['basic_user']) || !empty($arrOptions['basic_pass'])) {
curl_setopt($ch, CURLOPT_USERPWD, $arrOptions['basic_user'] . ':' . $arrOptions['basic_pass']);
}
$strBody = curl_exec($ch);
$arrResult['http_code'] = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($strBody === false) {
$arrResult['curl_error'] = curl_error($ch);
} else {
$arrResult['body'] = $strBody;
$tabJson = json_decode($strBody, true);
if (is_array($tabJson)) {
$arrResult['json'] = $tabJson;
}
}
curl_close($ch);
if ($arrResult['curl_error'] !== '') {
return $arrResult;
}
if ($arrResult['http_code'] >= 200 && $arrResult['http_code'] < 300) {
$arrResult['state'] = 'ok';
}
return $arrResult;
}
function fxChronotrackApiHttpGetJsonErrorMessage($arrHttp) {
if (!empty($arrHttp['curl_error'])) {
return $arrHttp['curl_error'];
}
$arrParts = array();
if (is_array($arrHttp['json'])) {
foreach (array('errorMessage', 'message', 'error_description', 'errorCode') as $strKey) {
if (!empty($arrHttp['json'][$strKey])) {
$arrParts[] = (string)$arrHttp['json'][$strKey];
}
}
if (!empty($arrHttp['json']['error'])) {
if (is_array($arrHttp['json']['error'])) {
foreach ($arrHttp['json']['error'] as $mixErr) {
if (is_string($mixErr) && trim($mixErr) !== '') {
$arrParts[] = $mixErr;
} elseif (is_array($mixErr)) {
if (!empty($mixErr['errorMessage'])) {
$arrParts[] = (string)$mixErr['errorMessage'];
} elseif (!empty($mixErr['message'])) {
$arrParts[] = (string)$mixErr['message'];
}
}
}
} elseif (is_string($arrHttp['json']['error'])) {
$arrParts[] = $arrHttp['json']['error'];
}
}
}
if (count($arrParts) > 0) {
return 'HTTP ' . intval($arrHttp['http_code']) . ' — ' . implode(' | ', array_unique($arrParts));
}
$strBody = trim((string)($arrHttp['body'] ?? ''));
if ($strBody !== '') {
return 'HTTP ' . intval($arrHttp['http_code']) . ' — ' . substr($strBody, 0, 400);
}
return 'HTTP ' . intval($arrHttp['http_code']);
}