Tuesday, May 31, 2011

Post in multiple Facebook fan page in one go


Hi,

This is yet another post related to Facebook API. In this I will be demonstrating how to make a wall post in multiple Facebook page of which you are an admin or a fan. A similar functionality I had posted in one of my previous post regarding posting in friends wall. In this also the basic idea is same. The main factor here is to generate the Facebook page list for the logged in user.

First we need yo understand the FQL(Facebook Query Language). Here we will be working with two tables, "page" and "page_admin". The page table is the master table and page_admin holds the mapping between Facebook user and the pages. Here is the query that I am using to fetch the list of pages:

$query="select page_id, name, pic_small, type from page where type<>'APPLICATION' and page_id in (select page_id from page_admin where uid =" . $uid . ")";

Here $uid is the Facebook user id of the currently logged in user. Also I have added a type<>'APPLICATION' in the where clause, so that I only get pages and not my applications. In Facebook, when you create a new App, it is also treated as a page. So I had to give the above validation.

Here is how the page looks:


Explanation:
There are 2 main factors to keep in mind in this application, first is fetching the page details whose admin I am by the following:

$fql    =   "select page_id, name, pic_small, type from page where type<>'APPLICATION' and page_id in (select page_id from page_admin where uid =" . $uid . ")";
            $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $page_list   =   $facebook->api($param);

Next is posting in the wall of the pages by:

foreach($_POST['pages'] as $key=>$val)
{
$tot++;
try {
$statusUpdate = $facebook->api('/'.$val.'/feed', 'post', array('message'=> $_POST['status'], 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<div style='color:red'>Error!! Status update failed.</div>";
}
if(!empty($statusUpdate)){
$success++;
}
}
echo "Status: $success status updated";

In above the array $_POST['pages'] has the ID's of the pages which are selected to be posted. I am keeping a $success variable to keep track of number of successful posts made.

Download: fb_page_publish.zip

5 comments:

  1. hmmm... when i post it posts as my personal account, so how would i change it to post to the various page walls under each individual page's name??

    ReplyDelete
  2. no sure what you mean. This is not posting in your wall, but in your page walls

    ReplyDelete
  3. When I use this, it posts onto the pages that I am an admin of, from my PERSONAL account, whereas it would be more useful if I were to be able to post to each page as the page (So post to PAGE as PAGE instead of posting to PAGE as INDIVIDUAL)

    ReplyDelete
  4. This application post my msg on other pages? If those page owners allow me to post on their pages.

    ReplyDelete