簡體   English   中英

Instagram API分頁

[英]Instagram API pagination

我正在使用以下代碼,該代碼可以很好地加載與#spproject標記匹配的所有圖像。 我想要做的是一次加載9張照片,然后使用ajax加載更多或只鏈接到上一頁/下一頁。 問題是我不太明白如何使用API​​,你能幫忙嗎?

碼:

<?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>

網站: http//www.spproject.info/

Instagram返回的JSON對象包含分頁變量,反過來又包含“next_url”變量。 next_url是您需要調用以獲取下一頁結果的API URL。

這是一個關於Instagram分頁的好教程 此外,未來的提示 - 不要在互聯網上發布您的API訪問代碼...

下面的(修訂)代碼應該是一個很好的起點。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM