What is the potential reason for the empty $page variable when using file_get_contents to extract data from a website in PHP?

The potential reason for the empty $page variable when using file_get_contents in PHP could be due to the website blocking the server's IP address or user agent. To solve this issue, you can set a user agent header in the HTTP request to mimic a browser and avoid being blocked by the website.

$url = 'https://www.example.com';
$options = [
    'http' => [
        'method' => 'GET',
        'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    ]
];

$context = stream_context_create($options);
$page = file_get_contents($url, false, $context);

if ($page === false) {
    echo "Failed to retrieve data from the website.";
} else {
    // Process the $page content here
}