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.
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.
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.
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.
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 :
To post a link on Facebook page wall :
Creates a note, but the $post_url must be PAGE_ID/notes.
To create a poll question on behalf of the Page, $post_url to PAGE_ID/questions.
To post a photo, change $post_url to PAGE_ID/photos.
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).
Here’s complete code of “process.php“:
Saran Chamling. (He loves the web technology, and enjoys designing, exploring and writing about it @saaraan.com).