dongtan3395 2014-11-02 18:00
浏览 30

Facebook Graph API加载更多/分页

OK, so I've been trying and looking around for a couple of days now, so I'll just ask here and see if anyone can help me solve/understand this :)

I'm getting pictures from a facebook album

function fbImgGalDisplay_func() {
//FB pageID and access token + history image gallery
$FBmain_imgGalID = 'ID_OF_GALLERY';
$FBaccess_token = 'MY_ACCESS_TOKEN';

//Get the JSON
$json_FBimgGal = @file_get_contents('https://graph.facebook.com/' . $FBmain_imgGalID . '/photos?limit=5&access_token=' . $access_token);

//Interpret data
$FBimgGal = json_decode($json_FBimgGal);

// open container for wall posts
echo '<div id="masContainer">';

    foreach ($FBimgGal->data as $img ){

        echo '<div class="fbImgGal_Img">';

        echo '<img src="' . $img->source . '" />';

        echo '</div>';
    }

// close container for wall posts
echo '</div>';

if($FBimgGal->paging){
    echo '<div value="' . $FBimgGal->paging->next . '" class="loadMoreBtn">THERE ARE MORE PICTURES</div>'; //$fbdata->paging->next;
}
}

So far, all good. I get images on the page. BUT, there are around 250 pictures in this album. I've tried increasing the limit, but it seems as though the Graph API has a limit of 100 (no matter what I put in the limit). So I was thinking that this is an excellent time to try and learn how to implement some sort of "infinite scroll" effect (so, when the user scrolls down, images keep appending to the existing masonry wall).

I tried this in jQuery (it requires the user to click the button, just for testing now)

$('.loadMoreBtn').click(function(){
    var nextVal = $('.loadMoreBtn').attr('value');

    $.ajax({
        url: nextVal,
        type: "GET",
        success: function(data){

            $.each( data, function(i, img){
                console.log( img.source );
            });

            console.log( nextVal );
        },
        error: function(xhr, status, error){
            console.log('there was an error');
        }
    });
});

But for some reason, when clicking the div, I can't get any data (other then 2 undefined in the console.log. The next link works).

What I was thinking was that when user clicks the div, the ajax sends the GET and uses success respons to keep populating the gallery.

Any help on this is greatly appreciated (and very needed! haha). Or if there is some better way of doing this? I'm open to suggestions... The idea here is to eventually add a window scroll to check the bottom offset, and THEN execute the "load more" (until there isn't a "next" URL in the respons).

*EDIT/SIDENOTE Is this the best way, from a security stand point, to do this? Considering that by using this technique, I have to pass the access token in the value (in the URL). So for a "nosy nelly", they could grab the access token... maybe not the best thing? :P

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败