Facebook在网站上提供

时间:2014-02-18 11:25:49

标签: php facebook-graph-api

我正在尝试在网站上获取一些fb Feed。我在网上搜索过,发现了一些我可以尝试的东西。失败了很多,我已经设法得到它。今天访问令牌不再有效,所以我得到了一个新的,它再次起作用。稍等一点(令牌仍然有效)之后它就不再存在(得到两条消息)。所以我再次搜索并找到了一个有效的新解决方案,我认为这给了我一个更长的有效令牌。这是php代码:

<?php
$limit = 3;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$curl = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$app_id.'&client_secret='.$app_secret;
//$authToken = file_get_contents($curl);
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
echo $data;
?>

这给了我以下结果: enter image description here

现在我正试图从中获取所有信息,但这里是我在foreach循环中出错的地方。这是我使用的代码:

<div id="footer">
    <h5>Laatste Facebook feeds</h5>
    <div class="wrapperfb">
        <?
            $counter = 0; 
            foreach($data->data as $d){
            if($counter==$limit)
            break;   

        ?>
        <div class="singlefb">
            <div class="imgfb">
                <a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
                <img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
                </a>
            </div>
            <div class="textfb">
                <span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
                <?=$d->from->name?></a></span><br/>
                    <span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
                    <br/>
                <?=$d->message?>
            </div>
       </div>
        <?
            $counter++;
            }
        ?>
    </div>
</div>

这是我得到的错误:

enter image description here

  

警告:为foreach()提供的参数无效   /customers/9/2/3/beach-korfbal.be/httpd.www/facebookFeeds.php上线   19

第19行是foreach循环:

  

foreach($ data-&gt;数据为$ d){

这可能是我正在看的傻事。请有人帮帮我。这是我需要完成的最后一件事来启动网站。

1 个答案:

答案 0 :(得分:0)

现在感谢Abhik Chakraborty的评论。

这是完整的代码:

<?php
$limit = 5;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = json_decode(file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}"));
?>
<div id="footer">
    <h5>Laatste Facebook feeds</h5>
    <div class="wrapperfb">
        <?
            $counter = 0; 
            foreach($data->data as $d){
            if($counter==$limit)
            break;   

        ?>
        <div class="singlefb">
            <div class="imgfb">
                <a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
                <img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
                </a>
            </div>
            <div class="textfb">
                <span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
                <?=$d->from->name?></a></span><br/>
                    <span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
                    <br/>
                <?=$d->message?>
            </div>
       </div>
        <?
            $counter++;
            }
        ?>
    </div>
</div>