What are alternative approaches or methods for accessing CSV data from websites that restrict access to browser-only downloads?

Many websites restrict access to CSV data by only allowing browser downloads, making it challenging to programmatically retrieve the data. One alternative approach is to use PHP to simulate a browser request and download the CSV file directly. This can be achieved by sending an HTTP request to the URL where the CSV file is located and then saving the response to a local file.

<?php

// URL of the CSV file
$url = 'https://example.com/data.csv';

// Path to save the downloaded CSV file
$localFilePath = 'data.csv';

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Save the CSV data to a local file
file_put_contents($localFilePath, $response);

echo 'CSV file downloaded successfully.';