如何在我的Facebook页面中显示最新的帖子到我的网站

use*_*247 7 php facebook facebook-graph-api facebook-c#-sdk

我在Facebook上有页面,我想在我的网站上显示从我的Feed /墙上的最新5个帖子.这该怎么做?我找到了这个解决方案......很容易

https://developers.facebook.com/docs/reference/plugins/like-box/
Run Code Online (Sandbox Code Playgroud)

有人指导我使用facebook api并自己做什么是最好的方法?

我使用php mysql来构建这个站点

Okk*_*kky 17

这是PHP代码.您需要将其放在模板中.

<ul>
<?php
//function to retrieve posts from facebook’s server
function loadFB($fbID){
    $url = "http://graph.facebook.com/".$fbID."/feed?limit=3";
    // Update by MC Vooges 11jun 2014: Access token is now required:
    $url.= '&access_token=YOUR_TOKEN|YOUR_ACCESS_SECRET';// *

    //load and setup CURL
     $c = curl_init($url);
     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    //get data from facebook and decode JSON
     $page = json_decode(curl_exec($c));
    //close the connection
     curl_close($c);
    //return the data as an object
     return $page->data;
}

/* Change These Values */
// Your Facebook ID
 $fbid = "190506416472588";
// How many posts to show?
 $fbLimit = 10;
// Your Timezone
date_default_timezone_set("America/Chicago");


/* Dont Change */
// Variable used to count how many we’ve loaded
 $fbCount = 0;
// Call the function and get the posts from facebook
 $myPosts = loadFB($fbid);


//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
    //only show posts that are posted by the page admin
    if($dPost->from->id==$fbid){
        //get the post date / time and convert to unix time
         $dTime = strtotime($dPost->created_time);
        //format the date / time into something human readable
        //if you want it formatted differently look up the php date function
         $myTime=date("M d Y h:ia",$dTime);
        ?>
        <ul>
            <li><?php echo($dPost->message) . $myTime; ?></li>
        </ul>
        <?php
        //increment counter
         $fbCount++;
        //if we’ve outputted the number set above in fblimit we’re done
         if($fbCount >= $fbLimit) break;
    }
}
?>
</ul>
Run Code Online (Sandbox Code Playgroud)

你必须做两件事来制定这个脚本.

  1. 确保您的服务器已启用cURL

  2. 您将更改脚本中的Facebook ID.

*您可以通过以下方式获取访问令牌:

$token = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials';
$token = file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不再起作用 - `print_r($ page);`在`函数loadFB()`中的`return`之前返回一个错误,指出需要访问令牌.具体来说,它返回:`stdClass Object([error] => stdClass对象([message] =>请求此资源需要访问令牌.[type] => OAuthException [code] => 104))`. (6认同)

Dee*_*ele 15

  1. 登录Facebook
  2. 转到facebok开发者部分" 应用程序 "
  3. 注册新的应用程序,您只需要注册新的应用程序,所有其他数据是可选的
  4. 从相同的" 应用 "部分复制您的App ID/API密钥和App Secret .
  5. facebook.phpbase_facebook.php文件从repo复制到您的服务器
  6. 使用多态查询 api,从facebook帐户请求墙内容

    require 'facebook.php';
    $facebook = new Facebook(array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
    ));
    
    $fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5');
    if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
        // display contents of $fbApiGetPosts["data"] array
    }
    
    Run Code Online (Sandbox Code Playgroud)

    将YOUR_APP_ID替换为您的应用ID,YOUR_APP_SECRET替换为您的应用密码,YOUR_FACEBOOK_ACCOUNT_ID替换为目标Facebook帐户,您希望从中获取帖子.

多态查询基本上是路径/ URL.更多信息在前面提到的facebook api 参考文档.

如果您的目标Facebook帐户墙是公开的,那么您将不需要任何其他内容来查看它们.