What are best practices for handling browser caching and ensuring data consistency when making HTTP requests in PHP?

When making HTTP requests in PHP, it is important to handle browser caching properly to ensure data consistency. One way to achieve this is by setting appropriate cache-control headers in the response. By doing so, you can control how browsers cache the response data and prevent outdated information from being displayed to users.

// Set cache-control headers to prevent browser caching
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

// Make HTTP request using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

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

// Output the data
echo json_encode($data);