What are the best practices for integrating PHP with external data sources on a website for dynamic content display?

When integrating PHP with external data sources on a website for dynamic content display, it is important to follow best practices to ensure security, efficiency, and reliability. One common approach is to use APIs provided by the external data source to retrieve and manipulate data. Additionally, consider implementing caching mechanisms to reduce the number of requests made to the external data source and optimize performance.

// Example of integrating PHP with an external API to retrieve and display dynamic content

// Set API endpoint and key
$api_endpoint = 'https://api.example.com/data';
$api_key = 'your_api_key_here';

// Make API request
$response = file_get_contents($api_endpoint . '?api_key=' . $api_key);
$data = json_decode($response);

// Display dynamic content
foreach ($data as $item) {
    echo '<div>';
    echo '<h2>' . $item->title . '</h2>';
    echo '<p>' . $item->description . '</p>';
    echo '</div>';
}