What alternative methods can be used to access files on external domains in PHP?

When accessing files on external domains in PHP, you may encounter restrictions due to security measures like cross-origin resource sharing (CORS). To overcome this limitation, you can use alternative methods like cURL or file_get_contents with stream contexts to retrieve the contents of files from external domains.

// Using cURL to access files on external domains
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/file.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

// Using file_get_contents with stream context
$context = stream_context_create(array(
    'http' => array('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')
));
$file_contents = file_get_contents('http://www.example.com/file.txt', false, $context);