What are the best practices for structuring PHP code to handle multiple API requests and display data for each image retrieved from Instagram?
When handling multiple API requests to retrieve images from Instagram, it is important to structure your PHP code in a way that efficiently processes each request and displays the data for each image retrieved. One way to achieve this is by using a loop to iterate through the API responses and extract the necessary information for each image. This allows you to organize the data in a structured format and display it accordingly on your webpage.
<?php
$access_token = 'YOUR_INSTAGRAM_ACCESS_TOKEN';
$user_id = 'YOUR_INSTAGRAM_USER_ID';
$api_url = 'https://graph.instagram.com/'.$user_id.'/media?fields=id,media_type,media_url,thumbnail_url&access_token='.$access_token;
$response = file_get_contents($api_url);
$data = json_decode($response);
if ($data) {
foreach ($data->data as $image) {
echo '<img src="' . $image->media_url . '" alt="Instagram Image">';
}
} else {
echo 'Error retrieving Instagram images';
}
?>