What are the performance implications of fetching content from external servers in PHP templates?

Fetching content from external servers in PHP templates can have performance implications due to the additional time required to make the external request and retrieve the content. To improve performance, consider caching the external content locally or using asynchronous requests to fetch the content in the background without blocking the main execution flow.

<?php
// Example of caching external content locally to improve performance
$cacheFile = 'cached_content.txt';
$cacheTime = 3600; // 1 hour

if (file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheTime) {
    // Load content from cache file
    $content = file_get_contents($cacheFile);
} else {
    // Fetch content from external server
    $content = file_get_contents('http://example.com/api/data');

    // Save content to cache file
    file_put_contents($cacheFile, $content);
}

echo $content;
?>