Update local file with remote, iff remote is newer
Cut and paste answer for those who want to
check if a remote file is more up to date than a local one, and update the local file if so:
// $remotePath = 'http://blahblah.com/file.ext';
// $localPath = '/usr/whatever/app/file.ext';
$headers = get_headers( $remotePath , 1 );
$remote_mod_date = strtotime( $headers['Last-Modified'] );
$local_mod_date = filemtime( $localPath );
if ( $local_mod_date >= $remote_mod_date ) {
// Local version up to date
} else {
// Remote file is newer
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remotePath);
// other options here, eg: curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
// handle error : curl_error($ch)
}
curl_close ($ch);
if ( $result ) {
// Update local file with remote file contents
file_put_contents( $localPath, $result );
}
}
With thanks to OP question here, and also this answer.
Created to solve automatic OIDC CA cert renewal (this, and this).