Programming

Get photos from Instagram using PHP & CURL

To display your Instagram photos on your site, you shall generate the access token.

Follow the documentation to generate the access token .

You can use the following function to get Instagram photos  or update it as per requirement.

    
function getInstagramFeeds($limit = 1){
        $api_url = "https://api.instagram.com/v1/users/self/media/recent/?access_token=your-access-token&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){
                /* Photo Type
                * thumbnail
                * low_resolution
                * standard_resolution
                */
            $items[] = array($feed->link, $feed->images->standard_resolution->url);
        }
        return $items;
    }

Here, $limit  is the number of photos you would like to display in your website.

   $instaItems = getInstagramFeeds(4);
    if(!empty($instaItems)):
    ?>
    <ul>
        <?php
        foreach($instaItems as $instaImage):
            $instaLink = $instaImage[0]; // Instagram photo link
            $instaImg = $instaImage[1]; //Instagram Image
        ?>
        <li> <a href="<?=$instaLink?>" target="_blank"><img src="<?php echo $instaImg; ?>"></a></li>
       <?php  endforeach; ?>
    </ul>
    <?php endif;

 

Views: 4542

Standard