Facebook使用PHP API发布到页面页面?

问题描述:

我正在尝试设置一个脚本来自动发布一个信息到Facebook页面,我是管理员。我有下面的代码,但目前,而不是作为页面管理员发布到页面的墙上,该帖子将从我的个人帐户的页面的访客帖子部分。关于我如何解决这个问题的任何想法?

I am trying to set up a script to automatically post a message to a Facebook page I am admin of. I have the code below but currently, instead of posting to the page's wall as the page admin, the post is going to the "visitor posts" section of the page from my personal account. Any ideas on how I can resolve this?

require_once('Facebook/autoload.php');

$fb = new Facebook\Facebook([
  'app_id' => 'myappid',
  'app_secret' => 'myappsecret',
  'default_graph_version' => 'v2.5',
]);

$pageID = 'mypageid';
$accessToken = 'myaccesstoken';

try {
  $publish = $fb->post('/' . $pageID . '/feed', 
     array(
       'access_token' => $accessToken,
       'message' => $status,
       'to' => $pageID
       )
   );
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}


您需要使用该页面访问令牌,而不是您的个人信息。

You need to use the page's access token, not your personal one.

使用您的个人访问令牌,调用 / me / accounts 。它将为您提供一个您是管理员的页面列表,并为每个页面访问令牌。

Using your personal access token, call /me/accounts. It'll give you a list of pages you're an administrator of, and access tokens for each one.