Check if remote file exists and when it was last updated.
string $uri: URI of remote file.
array|boolean Associative array of file data with the following elements:
TRUE if the file is not found. FALSE if a fault occurred.
function locale_translation_http_check($uri) {
try {
$response = Drupal::httpClient()
->head($uri)
->send();
$result = array();
// In case of a permanent redirected response, return the final location.
if ($previous = $response
->getPreviousResponse()) {
if ($previous
->getStatusCode() == 301) {
$result['location'] = $previous
->getLocation();
}
}
$result['last_modified'] = $response
->getLastModified() ? strtotime($response
->getLastModified()) : 0;
return $result;
} catch (BadResponseException $e) {
// Handle 4xx and 5xx http responses.
$response = $e
->getResponse();
if ($response
->getStatusCode() == 404) {
// File not found occurs when a translation file is not yet available
// at the translation server. But also if a custom module or custom
// theme does not define the location of a translation file. By default
// the file is checked at the translation server, but it will not be
// found there.
watchdog('locale', 'Translation file not found: @uri.', array(
'@uri' => $uri,
));
return TRUE;
}
watchdog('locale', 'HTTP request to @url failed with error: @error.', array(
'@url' => $uri,
'@error' => $response
->getStatusCode() . ' ' . $response
->getReasonPhrase(),
));
} catch (RequestException $e) {
// Handle connection problems and cURL specific errors (CurlException) and
// other http related errors.
watchdog('locale', 'HTTP request to @url failed with error: @error.', array(
'@url' => $uri,
'@error' => $e
->getMessage(),
));
}
return FALSE;
}