Instagram API分页 - php

我正在使用以下代码,该代码可以很好地加载与#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。

Here is a good tutorial与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>

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

使用PHP包含时的淡入淡出过渡 - php

我正在尝试使用jQuery或CSS(或其他方式!)在DIV中包含的php页面上创建淡入淡出或滑动过渡。我四处搜寻,发现了很多淡入淡出过渡的示例,这些实例彼此淡入淡出div或隐藏内容淡入淡出,但是这种情况略有不同。我有一个DIV,其内容由导航栏控制。选中后,每个页面都可以使用PHP成功地包含在div中,但我想使内容淡入和淡出。关于如何在页面更改之间进行漂亮过渡…

AJAX调用只能与$(document).on('click')一起使用 - php

我有一个显示数据库条目的表。用户能够为每一行打开一个弹出菜单。选项之一是删除数据库条目,并且该表应通过AJAX调用相应地刷新。只要有人单击#delete-toggle中的table-popup,我就会在HTML页面上进行AJAX调用(table-popup是div,当有人单击每行中存在的表中的table-edit-button时出现的表): <div …

将输入类型复选框关联到输入类型文本 - php

我有一个问题,我需要将输入类型复选框与输入类型文本关联。情况如下:从数据库中提取数据。 PK数据是复选框的值。当复选框选择输入类型的文本时,您可以在其中输入特定数字。现在的问题是,选中所有类型的复选框输入文本都会被激活。我希望通过选择复选框输入,仅启用与复选框相关联的输入。我的HTML代码(此代码创建一个输入复选框,并为数据库中的每个记录输入文本,而我要激活…

jQuery显示基于序列化哈希的隐藏选择菜单 - php

我试图通过使用Ajax和序列化的哈希值来使选择菜单显示隐藏。昨晚我有这个系统,但是我将#selector从表单更改为div,突然停止了运行。我不得不扩展表单以获取更多附加数据,并且不想为此立即序列化所有数据,因为这将给系统带来额外压力。该页面按预期工作。它显示了第一个选择,允许我选择一个选项,我可以看到AJAX发布,但是哈希值是空的,我相信这会破坏上面的PH…