What are some ways to fetch a file from a different domain in PHP?

When fetching a file from a different domain in PHP, you may encounter cross-origin resource sharing (CORS) restrictions that prevent the request from being successful. To overcome this issue, you can use PHP to act as a proxy server by fetching the file from the external domain and then serving it to your own domain.

<?php
// URL of the file to fetch from a different domain
$url = 'https://www.example.com/file.txt';

// Use cURL to fetch the file content
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$file_content = curl_exec($ch);
curl_close($ch);

// Output the fetched file content
echo $file_content;
?>