What are the best practices for displaying external data on a website using PHP?

When displaying external data on a website using PHP, it is important to sanitize and validate the data to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. One way to achieve this is by using PHP's htmlentities() function to encode any special characters in the data before displaying it on the webpage.

<?php
// Get external data
$externalData = file_get_contents('https://example.com/data.json');

// Sanitize and validate the data
$cleanData = htmlentities($externalData);

// Display the data on the webpage
echo $cleanData;
?>