Written by Saran on February 14, 2012, Updated July 7, 2015
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