What are the best practices for loading and examining external website content in PHP?

When loading and examining external website content in PHP, it is important to use secure methods to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. One of the best practices is to use PHP cURL functions to fetch the external content and sanitize the data before displaying it to the user.

$url = 'https://www.example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

// Sanitize the output before displaying it to the user
$sanitized_output = htmlspecialchars($output);

echo $sanitized_output;