Post to Facebook Page Wall Using PHP + Graph

In this post you will learn how to post to Facebook page wall (Not User Wall) using PHP and Facebook API. To understand this article you must have knowledge of Facebook application development and their API usage. Before we begin I assume you have created Facebook Application and you have a running Facebook Page where you want  your message to appear.  
Facebook has updated their SDK to 4.0. Please follow new Facebook SDK 4.0 tutorial here
Basically we have three PHP pages in this tutorial. Configuration file (config.php), Form Page (index.php) and Process Page (process.php). Index page is a HTML form with message box and list of user pages, where user selects a page and post some messages, which gets posted to process.php, if everything goes well, it shows a success message and user message will appear on Facebook Page wall. Other files such as Facebook PHP SDK, css file etc are included in sample download file.

Configuration

Config.php stores variables, such as facebook Application ID, secret, return url etc. Change these settings with your own. Notice include_once("inc/facebook.php"); , inc folder contains Facebook PHP SDK files, which can be downloaded from https://github.com/facebook/php-sdk, but I have already included these files in downloadable zip file at the bottom of the page.[cc lang="php"] $appId, 'secret' => $appSecret ));$fbuser = $facebook->getUser(); ?> [/cc]

Front Page

Index.php contains a message form for this demo, user is redirected to facebook authentication page, where s/he is required to authenticate and grant mainly two extended permissions publish_stream and manage_pages. It seems permission manage_pages is required to read user pages, and to post on page wall, application requires publish_stream.Once user grants these permissions, user is redirected back to index page, where s/he is presented with a form containing a message input box and list of his own Facebook pages. You see, to list user pages, I have used FQL (Facebook Query Language), FQL is Facebook's own query language and it is very similar to MySQL queries, with FQL it is possible to retrieve more information in a way that we can't with just Graph API (we will get back to it soon). On form submission the data is sent to process.php.[cc lang="php"] api(array( 'method' => 'fql.query', 'query' => $fql_query )); } catch (FacebookApiException $e) { echo $e->getMessage(); } }else{ //Show login button for guest users $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); echo ''; }if($fbuser && empty($postResults)) { /* if user is logged in but FQL is not returning any pages, we need to make sure user does have a page OR "manage_pages" permissions isn't granted yet by the user. Let's give user an option to grant application permission again. */ $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); echo 'Could not get your page details, make sure you have created one!'; echo 'Click here to try again!'; }elseif($fbuser && !empty($postResults)){//Everything looks good, show message form. ?>Post to Facebook Page Wall Demo

Post to Facebook Page Wall

Choose a page to post!

[/cc]

Posting to Facebook Page Wall

After receiving page id and message variables from index page, all we need to do is use page id to select a page and send a POST request to PAGE_ID/feed with the publish_stream permission. This script tries to post message to selected user Facebook page wall, and displays a success message.Main thing to note here in process.php is $post_url and $msg_body array variable, these two things define what you are going to post on Facebook page wall, for example you may want to automatically post notes, events, questions, status message, photos or videos.The following snippet creates a status message :[cc lang="php"] $msg_body = array( 'message' => 'message for my wall' ); [/cc]To post a link on Facebook page wall :[cc lang="php"] $msg_body = array( 'link' => 'http://www.saaraan.com', 'message' => 'message for my wall' ); [/cc]Creates a note, but the $post_url must be PAGE_ID/notes.[cc lang="php"] $msg_body = array( 'subject' => 'Subject of Note', 'message' => 'message for my wall' ); [/cc]To create a poll question on behalf of the Page, $post_url to PAGE_ID/questions. [cc lang="php"] $msg_body = array( 'question' => 'Do you like saaraan.com?', //Question 'options' => array('Yes I do','No I do Not','Can not Say'), //Answers 'allow_new_options' => 'true' //Allow other users to add more options ); [/cc]To post a photo, change $post_url to PAGE_ID/photos.[cc lang="php"] $msg_body = array( 'source' => '@'.realpath('myphoto/somephot.gif'), 'message' => 'message for my wall' ); [/cc]You can also save your photos, notes, status messages as unpublished by issuing 'published'=>'false'. Or set scheduled_publish_time for the post : 'scheduled_publish_time'=>'1333699439' (UNIX timestamp). [cc lang="php"] $msg_body = array( 'source' => '@'.realpath('myphoto/somephot.gif'), 'message' => 'message for my wall', 'published' => 'false', //Keep photo unpublished 'scheduled_publish_time' => '1333699439' //Or time when post should be published ); [/cc]Here's complete code of "process.php": [cc lang="php"] $userMessage, 'name' => 'Message Posted from Saaraan.com!', 'caption' => "Nice stuff", 'link' => 'https://www.sanwebe.com/assets/ajax-post-on-page-wall', 'description' => 'Demo php script posting message on this facebook page.', 'picture' => 'https://www.sanwebe.com/templates/saaraan/images/logo.png' 'actions' => array( array( 'name' => 'Saaraan', 'link' => 'http://www.saaraan.com' ) ) ); *///posts message on page statues $msg_body = array( 'message' => $userMessage, );if ($fbuser) { try { $postResult = $facebook->api($post_url, 'post', $msg_body ); } catch (FacebookApiException $e) { echo $e->getMessage(); } }else{ $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); header('Location: ' . $loginUrl); }//Show sucess message if($postResult) { echo 'Message Posted'; echo '
'; echo '

Your message is posted on your facebook wall.

'; echo 'Back to Main Page Visit Your Page'; echo '
'; echo ''; } }?> [/cc]Download Demo
  • Hello Folks..... This article seems me give an help to write a php web application which queries FB Graph library, right ? But in reading it, it seems me I have to write a FB Application, but really I have not any idea how to code it and which is its utility and need; could someone clarify me ? I also know about a windows executable application which retrieves and query FB Graph but it does not need any Secret Application credential..... Advanced thanks to everybody will reply me !!!
New question is currently disabled!