Skip to content

Instantly share code, notes, and snippets.

@inadeemkhan
Created May 23, 2023 10:32
Show Gist options
  • Save inadeemkhan/e37e90c07efbd7d800bf75343f7629d7 to your computer and use it in GitHub Desktop.
Save inadeemkhan/e37e90c07efbd7d800bf75343f7629d7 to your computer and use it in GitHub Desktop.
PHP Curl code to get the Instagram image.
// PHP code:
function getInstagramFeeds($limit = 1){
$api_url = "https://graph.instagram.com/me/media?fields=id,media_type,media_url,username,timestamp&access_token=IGQ****&count=".$limit;
$connection_c = curl_init(); // initializing
curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // Return the result, do not print
curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $connection_c ); // Connect and get json data
curl_close( $connection_c ); // Close connectionßßß
$insta = json_decode( $json_return ); // Decode and return
foreach($insta->data as $feed){
$items[] = array($feed->media_url, $feed->media_type);
}
return $items;
}
// HTML Code:
<?php
$instaItems = $block->getInstagramFeeds(40);
if(!empty($instaItems)):
?>
<ul>
<?php
foreach($instaItems as $instaImage):
$instaType = $instaImage[1]; // Instagram photo link
$instaImg = $instaImage[0]; //Instagram Image
?>
<?php if ($instaType == "VIDEO"): ?>
<video width="295" height="365" controls>
<source src="<?php echo $instaImg; ?>" type="video/mp4">
</video>
<?php endif; ?>
<?php if ($instaType == "IMAGE"): ?>
<li><img src="<?php echo $instaImg; ?>"></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment