简体   繁体   中英

Instagram API pagination

I am using the below code which works well at loading all the images that match the #spproject tag. What i'd like to do is load 9 photos at a time and then either load more with ajax or just link to previous/next pages. Problem is i dont quite understand how to do it using the API, can you help?

CODE:

<?PHP
function get_instagram($next=null,$width=160,$height=160){

    if($next == null ) {
        $url = 'https://api.instagram.com/v1/tags/spproject/media/recent?access_token=[TOKEN]&count=10';
    }
    else {
        $url .= '&max_tag_id=' . $next;
    }

    //Also Perhaps you should cache the results as the instagram API is slow
    $cache = './'.sha1($url).'.json';
    //unlink($cache); // Clear the cache file if needed

    if(file_exists($cache) && filemtime($cache) > time() - 60*60){
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache));
    }else{
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }

    $result = '<ul id="instagramPhotos">'.PHP_EOL;
    if (is_array($jsonData->data))
    {
        foreach ($jsonData->data as $key=>$value)
        {
            $result .= '<li><div class="album">
        <figure class="frame">
            <a href="'.$value->link.'" target="_blank"><i><img src="'.$value->images->standard_resolution->url.'" alt="" width="'.$width.'" height="'.$height.'" name="'.$value->user->username.'"></i></a>
        </figure>
        <span class="count">#SPproject</span>
        <a href="http://www.instagram.com/'.$value->user->username.'" target="_blank"><figcaption class="name">'.$value->user->username.'</figcaption></a>
    </div></li>'.PHP_EOL;
        }
    }
    $result .= '</ul>'.PHP_EOL;

    if(isset($jsonData->pagination->next_max_tag_id)) {
        $result .= '<div><a href="?next=' . $jsonData->pagination->next_max_tag_id . '">Next</a></div>';
    }

    return $result;
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>#SPproject - A worldwide instagram idea</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="normalize.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var totalPhotos = $('#instagramPhotos > li').size();
            $('#result').text('Total tagged images: '+totalPhotos);
        });
    </script>
</head>
<body>
    <div id="container">
        <?=get_instagram(@$_GET['next']);?>

        <div id="result"></div>
    </div>
</body>
</html>

website: http://www.spproject.info/

The JSON object which Instagram is returning contains a pagination variable and in turn, a "next_url" variable. next_url is the API URL you need to call to get the next page of results.

Here is a good tutorial on pagination with Instagram. Also, a tip for the future - don't post your API access codes on the internet...

The (revised) code below should be a good starting point for you.

<?PHP
function get_instagram($next=null,$width=160,$height=160){

    $url = 'https://api.instagram.com/v1/tags/spproject/media/recent?access_token=[token]&count=9';

    if($url !== null) {
        $url .= '&max_tag_id=' . $next;
    }

    //Also Perhaps you should cache the results as the instagram API is slow
    $cache = './'.sha1($url).'.json';
    //unlink($cache); // Clear the cache file if needed

    if(file_exists($cache) && filemtime($cache) > time() - 60*60){
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache));
    }else{
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }

    $result = '<ul id="instagramPhotos">'.PHP_EOL;
    foreach ($jsonData->data as $key=>$value) {
        $result .= '<li><div class="album">
        <figure class="frame">
            <a href="'.$value->link.'" target="_blank"><i><img src="'.$value->images->standard_resolution->url.'" alt="" width="'.$width.'" height="'.$height.'" name="'.$value->user->username.'"></i></a>
        </figure>
        <span class="count">#SPproject</span>
        <a href="http://www.instagram.com/'.$value->user->username.'" target="_blank"><figcaption class="name">'.$value->user->username.'</figcaption></a>
    </div></li>'.PHP_EOL;;
        //$result .= '<li><a href="'.$value->link.'"><img src="'.$value->images->standard_resolution->url.'" alt="" width="'.$width.'" height="'.$height.'" name="'.$value->user->username.'" /></a></li>'.PHP_EOL;
    }
    $result .= '</ul>'.PHP_EOL;

    if(isset($jsonData->pagination->next_max_tag_id)) {
        $result .= '<div><a href="?next=' . $jsonData->pagination->next_max_tag_id . '">Next</a></div>';
    }

    return $result;
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>#SPproject - A worldwide instagram idea</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="normalize.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var totalPhotos = $('#instagramPhotos > li').size();
            $('#result').text('Total tagged images: '+totalPhotos);
        });
    </script>
</head>
<body>
    <div id="container">
        <?=get_instagram(@$_GET['next']);?>

        <div id="result"></div>
    </div>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM