簡體   English   中英

Facebook PHP API代碼無法與“頁面”一起使用以獲取Wall帖子

[英]Facebook PHP API code not working with “Pages” to get Wall posts

<?php
require '../src/facebook.php';
try
{
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '120875194666085',
  'secret' => '0272027b5c5c1dabde81096497970c56',
  'scope' => 'read_stream',
));

$user = $facebook->getUser();


if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me/feed');

  } catch (FacebookApiException $e) {
    error_log($e);

    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope' => 'read_stream'));
}

}
catch(FacebookApiException $e){}
?>
<?php

     if ($user): ?>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <?php if ($user): ?>

    <?php 
        for($i = 0; $i < 25; $i++)
           {
        echo "<br />"; 
        echo $user_profile['data'][$i]['from']['name'];
              echo " : "; 
        echo $user_profile['data'][$i]['message']; 
           }
    ?>
    <?php endif ?>

這對於普通帳戶來說效果很好,但是當我嘗試使用Facebook“頁面”帳戶登錄時,它根本不起作用。 在這件事上有幫助嗎? 另外,當我到達Facebook Graphs API文檔時,即使是那些鏈接也無法為我提供Pages牆的正確提要。 從我收集的資料來看,我必須https://graph.facebook.com/ [公司頁面ID] / feed ?? access_token = [access_token]才能使其正常工作(我不確定如何將其翻譯成Facebook API。

只要您有任何access_token,就應該可以訪問粉絲專頁供稿。

如果您具有已登錄的有效授權用戶

$fan_page_feed = $facebook->api('/COMPANY_PAGE_ID/feed');

將返回一個包含提要數據的JSON對象。

或者,您可以使用應用程序access_token來獲取信息。 此示例從Facebook Platfrom頁面獲取提要(為簡潔起見,腳本還假定一切都將正常運行-您應該根據自己的喜好實施錯誤處理):

<html><head></head>
<body>
<pre>
<?php
require ('facebook.php');

$facebook = new Facebook(array(
            'appId'  => 'YOUR_APP_ID',
            'secret' => 'YOUR_APP_SECRET',
            'cookie' => true
        ));

$result = $facebook->api('oauth/access_token', array('client_id'=>'YOUR_APP_ID', 'client_secret'=>'YOUR_APP_SECRET', 'grant_type'=>'client_credentials'));
// $result should contain 'access_token=VALID_ACCESS_TOKEN'
$access_token = explode('=',$result);
$fan_page_feed = $facebook->api('/19292868552/feed', array('access_token'=>$access_token[1]));

print_r($fan_page_feed);
?>
</pre>
</body>
</html>

暫無
暫無

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

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