簡體   English   中英

使用圖譜API從FB獲取最新帖子

[英]getting latest posts from fb using graph api

我想從fb獲取最新帖子,並且需要顯示在我的網站上(magento)。 我已經在Facebook中注冊了一個應用,並嘗試使用url來獲取帖子,但是它給出了空數組

    require_once(Mage::getBaseDir('lib') . '/facebook/facebook.php');
    $facebook = new Facebook(array(
              'appId'  => 'xxxxxxxxxxxx',
              'secret' => 'xxxxxxxxxxxxxxxxxxxx',
            ));
    $fbid = "xxxxxxxxxx";
    $secret = "xxxxxxxxxxxxxxxxxxxxxxx";
    $token = 'https://graph.facebook.com/oauth/access_token?client_id='.$fbid.'&client_secret='.$secret.'&grant_type=client_credentials';
    $token = file_get_contents($token);
    $posts = json_decode(
                file_get_contents('https://graph.facebook.com/' . $fbid . '/feed?
                    access_token=' . $token
                )
            );

但這給了一個空數組,您能幫我得到結果嗎,為什么給空呢?

為了從Facebook讀取Feed,您必須將用戶登錄到Facebook,並要求用戶提供read_stream權限。

該供稿將是已登錄用戶的供稿,可能不適用於您網站的所有用戶,除非您網站的每個用戶看到自己的供稿...

如果您有有效的訪問令牌,則可以使用php graph api從任何公共頁面獲取提要。可以使用file_get_contents或curl方法調用api。

function curl_get_file_contents($URL) {
$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $URL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
 curl_setopt($ch, CURLOPT_TIMEOUT, 400);
 $contents = curl_exec($ch);
 $err  = curl_getinfo($ch,CURLINFO_HTTP_CODE);
 curl_close($ch);
$contents=json_decode($contents,true);
if ($contents) return $contents;
else return FALSE;
}

$access_token = 'your accesstoken';
$url = " https://graph.facebook.com/$page_id/feed?access_token=$access_token";
$posts = curl_get_file_contents($url);

現在$ posts將擁有頁面上的所有最近發布的帖子,您可以使用foreach來獲取每個帖子。

  1. 創建App Developer Facebook頁面

您可以通過圖表獲取實時訪問令牌。 然后代碼示例:

<ul>
<?php

$page_name = '{PAGE_NAME}'; // Example: http://facebook.com/{PAGE_NAME}
$page_id = '{PAGE_ID}'; // can get form Facebook page settings
$app_id = '{APP_ID}'; // can get form Developer Facebook Page
$app_secret = '{APP_SECRET}'; // can get form Developer Facebook Page
$limit = 5;

function load_face_posts($page_id, $page_name, $app_id, $app_secret, $limit, $message_len) {
    $access_token = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials";
    $access_token = file_get_contents($access_token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
    $access_token = str_replace('access_token=', '', $access_token);
    $limit = 5;
    $data  = file_get_contents("https://graph.facebook.com/$page_name/posts?limit=$limit&access_token=$access_token");
    $data = json_decode($data, true);
    $posts = $data[data];
    //echo sizeof($posts);

    for($i=0; $i<sizeof($posts); $i++) {
        //echo $posts[$i][id];
        $link_id = str_replace($page_id."_", '', $posts[$i][id]);
        $message = $posts[$i][message];

        echo ($i+1).". <a target='_blank' href='https://www.facebook.com/AqualinkMMC/posts/".$link_id."'>".$message."</a><br>";
    }
}

load_face_posts($page_id, $page_name, $app_id, $app_secret, $limit, $message_len);
?>
</ul>

暫無
暫無

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

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