Latest photos from instagram on PHP

You block advertising

Please disable ad blocker

Read more

For display the latest photos from instagram on PHP, you need to write 2 functions. In the first, we turn to the instagram API:

	// Get our data
    function fetchData( $url ){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
    }

In the second one – we set the number of photos and print the result on the page:

    function instagram_feed( $instagram_token, $instagram_id ) {
        if ( $instagram_token != '' && $instagram_id != '' ){
            // Pull and parse data.
            $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" );
            $result = json_decode( $result );
            $limit = 10; // Amount of images to show
            $i = 0;

            foreach ( $result->data as $post ){
                if ( $i < $limit ){
                    echo '<a class="insta-wrap__link" rel="nofollow" target="_blank" href="' . $post->link . '">';
                    echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="">';
                    echo '</a>';
                    $i ++;
                }
            }
        }
    }

In $post->images->thumbnail->url You can replace the ‘thumbnail’ with different values to get photos with different resolutions:

  • thumbnail – 320*320px;
  • low_resolution – 150*150px;
  • standard_resolution – 640*640px;

And, finally, run the function where we need it:

<div class="instagram-feeds">
    <?php
        $instagram_token = 'xxx';
        $instagram_id = 'xxx';
        if ( $instagram_token !='' && $instagram_id != '' ) { ?>
            <div class="insta-wrap">
                <?php instagram_feed( $instagram_token, $instagram_id ); ?>
            </div>
        <?php }
    ?>
</div>

Variable $instagram_token – it’s Instagram Access Token (how to get it you can find out in this article. $instagram_id = you can find, for example, here. Just replace ‘xxx’ with your data and you will get a list of your latest photos from the instagram.

Full code:

<?php
    // Get our data
    function fetchData( $url ){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
    }
    function instagram_feed( $instagram_token, $instagram_id ) {
        if ( $instagram_token != '' && $instagram_id != '' ){
            // Pull and parse data.
            $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" );
            $result = json_decode( $result );
            $limit = 10; // Amount of images to show
            $i = 0;

            foreach ( $result->data as $post ){
                if ( $i < $limit ){
                    echo '<a class="insta-wrap__link" rel="nofollow" target="_blank" href="' . $post->link . '">';
                    echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="">';
                    echo '</a>';
                    $i ++;
                }
            }
        }
    }
?>

<div class="instagram-feeds">
    <?php
        $instagram_token = 'xxx';
        $instagram_id = 'xxx';
        if ( $instagram_token !='' && $instagram_id != '' ) { ?>
            <div class="insta-wrap">
                <?php instagram_feed( $instagram_token, $instagram_id ); ?>
            </div>
        <?php }
    ?>
</div>
Demo

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Rate our article
3.5/54
Comments (0)
Leave a Reply

Your email address will not be published. Required fields are marked *