cpcdev
cpcdev

Reputation: 1222

Facebook PHP SDK v5 Post to Fan Page

I would like to use the Facebook PHP SDK v5 to automatically post to a FAN PAGE. I have the code below which successfully posts to my own wall, but how do I alter the code to post to my fan page? From what I've read, I need to pass in the page id of the fan page?

$params["message"] = 'test';
$params["link"] = 'https://example.com';
$params["picture"] = 'https://example.com/images/logo_hod.jpg';
$params["description"] = 'testtt';

$access_token = $accessToken;

try {
    $response = $fb->post('/me/feed', $params, $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$graphNode = $response->getGraphNode();

echo 'Posted with id: ' . $graphNode['id'];

I tried replacing this line:

$response = $fb->post('/me/feed', $params, $access_token);

with this to substitute in the fan page id:

$response = $fb->post('/229107363768165/feed', $params, $access_token);

and got the error:

Graph returned an error: Unsupported post request.

UPDATE: I also made the app "public" in an attempt to get past the "unsupported post request" error. No luck.

Upvotes: 0

Views: 1017

Answers (1)

Maurice
Maurice

Reputation: 156

i think your $params is not right. I use this:

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.2',
  ]);

$params = [
  'link' => 'https://example.com',
  'photo' => 'https://example.com/images/logo_hod.jpg',
  'message' => 'A test post!',
];

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->post('/me/feed', $params, '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

Post Links Using the Graph API

I assumes that you've already obtained an access token and the access token must have the publish_actions permission for this to work.

Upvotes: 1

Related Questions