Automatically post to Facebook from PHP script
This post guides you through the creation of a Facebook application that can automatically post messages and other types of content on your Facebook wall.
Building a php script that automatically posts status updates on your wall requires the following steps:
- Download Facebook API
- Register a Facebook app
- Give the app rights to post on your wall
- Build the post to Facebook script
Get the Facebook API
First step is to download a general Facebook communication class from Glithub.
Download the files and upload the files to your site – e.g. to a folder called fb.
The file containing the Facebook class is “facebook.php”
Register a facebook app
Facebook only allows registrated apps to interact with Facebook accounts and pages. The next step is therefore to register your app.
You can register a new app on this page
To register a Facebook App you need to provide: Site name, Site url (with ending “/”) and the locality of your application.
If everything is OK, you get to a registration conformation screen, where you get the two important informations: App ID and App Secret that you need in the next steps.
Give the application the rights to access your wall
Next step is to build a small script that only has one purpose, identify itself with the new App ID and App Secret to your Facebook account, so you can allow it to directly communicate with your Facebook page.
If you skip this step, you need to manually allow the script to access your Facebook account each time it’s trying to post something, which isn’t that smart…
Simply insert your App ID and App Secret in this script below and save the script as “fb_access.php” or something similar.
Next step is to upload the script to the same directory on your server where you uploaded the “facebook.php” class. In this example we’ll call the script “fb_access.php”
Finally you need to upload the script to your server, and access the script from your web browser: http://<yoursite>/fb/fb_access.php.
Click the allow button, and you’re ready to build a script that posts to Facebook.
Depending on your Facebook configuration you might get an error message when accessing fb_access.php in your browser.
This typically means that you need to provide a url to the website connected in the application. Simply click the link “application settings editor” and provide: Site URL in the “Web site” tab, and try to load the fb_access.php file again in your browser.
Build the Post to Facebook script
Now that your application has access to your Facebook account, and we have the general facebook.php class, it’s very easy to build a small script that can post messages to your Facebook wall.
And that’s it. Try to access the script from your browser and check if a post is added to your Facebook wall.
Please notice that if you controls multiple Facebook pages, this script will post to the wall of your personal profile. If you want to post to a specific page, you need to replace “/me/” with the ID of the specific page.
Another important information is, that you will get an error message if you try to post the same message twice. So remember to change the message, when you’re testing the script.
This post guides you through the initial Facebook application registration, and basic script for automatically posting content to a Facebook wall.
Next step could be to integrate this script in your CMS system, so a new post is automatically added to Facebook every time you’re updating the content on your site.
This script is very basic and only adds a status message in plain text. However you can naturally also post images, links, and other informations. You can see more about the additional informations you can include here.