Are there any best practices for automatically determining the Content-Type based on the response content in PHP?
When making HTTP requests in PHP, it is important to accurately determine the Content-Type of the response content. One way to automatically determine the Content-Type based on the response content is to use the `get_headers()` function to retrieve the headers of the response and parse the Content-Type header. This can be done by checking if the Content-Type header exists and extracting the MIME type from it.
$url = 'https://www.example.com/api';
$headers = get_headers($url, 1);
if (isset($headers['Content-Type'])) {
$contentType = $headers['Content-Type'];
// Extract the MIME type from the Content-Type header
$mime = explode(';', $contentType)[0];
echo "Content-Type: $mime";
} else {
echo "Content-Type header not found in response";
}