2

I'm using facebook's php SDK and I'm trying to post on a page which is managed/administered by the logged in user. I've granted the publish_stream and manage_pages permission. And I want that the post should look like it is made by page rather than admin or logged in user. I've tried several helps but no one is working. Here is my part of my existing code:

require './php-fb-sdk/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook( array('appId' => 'xxxx', 'secret' => 'zzzz'));

// Get User ID
$user = $facebook -> getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

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

$params = array("scope" => array("manage_pages", "publish_stream"));
// Login or logout url will be needed depending on current user state.
if ($user) {
    // Fetch the viewer's basic information
    $basic = $facebook -> api('/me');

    $permissions = $facebook -> api("/me/permissions");
    if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) {

        $admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'"));
        if (count($admin_pages) > 0) {

            $post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
            echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>';
        } else {
            echo '<hr> You are not admin of any fb fan page<hr>';

        }
        //print_r($admin_pages);

    } else {
        // We don't have the permission
        // Alert the user or ask for the permission!
        header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream")));
    }
    $logoutUrl = $facebook -> getLogoutUrl();
} else {
    //$statusUrl = $facebook->getLoginStatusUrl();
    $loginUrl = $facebook -> getLoginUrl($params);
    $statusUrl = $loginUrl;
}

Using the above code I can post on the page but it looks like it is made by user.

However if I use facebook JS SDK then I see the post looks like it is made by the page.

var data=
            {
                caption: 'My Caption',
                message: 'My Message'
            }

            FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted);
            }

Any help or suggestions will be greatly appreciated.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90

1 Answers1

2

To post on a page on behalf of the page, you need to use the page access token. Your current call:

$facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));

is using the default (user) access token.

To get the page access token, make this call before publishing post:

\GET /{page-id}?fields=access_token

this will get you a page access token in the result, then simply use this to make your publishing feed call, just like this-

$facebook -> api(
  '/' . $admin_pages[0]["page_id"] . '/feed', 
  'post', 
  array(
    "caption" => "From web", 
    "message" => "This is from my web at: " . time(),
    "access_token" => '{page_access_token}'
  )
);

(If needed you can also get a never expiring token of your page, check here how to: What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server)

Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90