Tuesday, 26 March 2013

Publish facebook page status or wall post automatically


Publish facebook page status or wall post automatically


Facebook page is now very popular. Many companies and people create facebook page for their own publicity. Suppose your company has 100 products and they want to maintain 100 facebook pages, then isn’t it very difficult to update status all of the pages manually?
In this article, I’ll show and discuss about 2 facebook api, using that you can easily update your facebook page status or publish wall post on that page automatically
I assume that you know how to set cron from hosting admin panel, I also assume that you have intermediate knowledge of facebook application development and their api usage.
fb_page
At first you’ve to setup a new facebook application. In the application setting look Authentication and in the Authentication setting must tick Facebook Pages
fb_app_setting
Now save the api and secret key either in your database or config file.
Update Status or Publish in Wall of your facebook page:
To update status of a facebook page, you must have to become an admin of that page. Now add the newly created application on that facebook page.
To add application in your facebook page: visit http://www.facebook.com/apps/application.php?id=XXXXXXXXX where XXXXXXXX is the id of your newly created application. Now click Add to my page
fb_app_add
This will show you a list of pages. If you don’t see your desire facebook page, remember either you don’t an admin of that page or you already added this app on that page.
To update page status, you need extended permission. Write a php script for your facebook application and run that script inside facebook. Like http://apps.facebook.com/yourapps/permission.php
01
02
03
04
05
06
07
08
09
10
11
12
13
//filename: permission.php
<script type="text/javascript">
    function addFbAutostatus(id){
        Facebook.showPermissionDialog('status_update', null, true, [id]);
    }
    function addFbAutoWallPostPublish(id){
        Facebook.showPermissionDialog('publish_stream,offline_access', null, true, [id]);
    }
</script>
 
<a href="#" onclick="addFbAutostatus('XXXXXXXXXXXx'); return false;">Page Status Update</a> //replace XXXXXXXX by your facebook page id
 
<a href="#" onclick="addFbAutoWallPostPublish('XXXXXXXXXXXx'); return false;">Page Wall Update</a> //replace XXXXXXXX by your facebook page id
In the canvas page, you’ll see the link Page Status Update, click it and accept the permission if you only update status. If you want to publish photos/videos on page wall then you’ve to click Page Wall Update and to accept those permissions.
Now write a script that will update your facebook page status or publish wall post:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//filename: cron.php
include_once "config.php";
$facebook   =   new Facebook($config['api_key'], $config['secret_key']);
 
$status      =   "Sample Status";
$pageid      =   XXXXXXXXX; //replace it by your desire facebook page id
try {
  $status = $facebook->api_client->users_setStatus($status, $pageid);
}
catch(Exception $o ){
    print_r($o);
}
 
/*if you want to publish wall post in facebook page, then you have to use another method and remember for this method call you've set your user id and session key, here i just shown how to publish status using stream_publish method, but you can also publish video, image as wall post using stream_publish method
$user = your user id and $session_key = your facebook session key, you can obtain them by
$user = $_REQUEST['fb_sig_user'];
$session_key = $_REQUEST['fb_sig_session_key'];
And don't forget to save them in database, because you'll need them when you will use cron.php from offline
*/
try {
      $facebook->set_user($user, $session_key);
      $facebook->api_client->stream_publish($status, null, null,  $item['pageid']);
}
catch(Exception $o ){
    print_r($o);
}
Now run in browser: http://yoursite.com/yourfbappdir/cron.php
And check the page, you’ll see you facebook page’s status updated. :) If you want to predefine many statuses and update them automatically, then create an admin panel and database from where you’ll add many statuses, and make the cron.php more dynamic and set it as cron. So that cron.php catch statuses one by one from database and based on publish-date it will update your facebook page by updated status.
To learn more about stream_publish method: Click Here
To learn more about users_setStatus: Click Here

No comments:

Post a Comment