What are some best practices for utilizing the file_get_contents() function in PHP for data extraction?

When using the file_get_contents() function in PHP for data extraction, it is important to handle errors and exceptions properly to ensure the reliability of your code. One best practice is to check if the function returns false, indicating an error, and handle it accordingly. Additionally, it's a good idea to set a timeout for the function to prevent it from hanging indefinitely.

$url = 'https://example.com/data.json';
$contents = @file_get_contents($url, false, stream_context_create(['http' => ['timeout' => 5]]));

if ($contents === false) {
    // Handle error
    die('Error fetching data');
}

// Process the retrieved data
$data = json_decode($contents, true);