How to Auto Post on Facebook with PHP
In a previous post I wrote about post automation on Twitter using PHP. The whole procedure is similar. You have to create a Facebook app and you need a PHP SDK to communicate with Facebook API.
However, the Facebook post automation is much more complicated than the relative process for Twitter. This is not a surprise, as Facebook itself is much more complicated than Twitter. For example, except of a Facebook Personal Profile, you may have Fan Pages or Business Pages.
A significant difference is that Twitter Access Tokens will never expire (according to current Twitter policy), while Facebook Access Tokens will expire in 60 days.
Share a Link to Facebook using PHP
Here is the code you need to share a link to Facebook using PHP. With small changes you can use this code to post just a message (without link), or to upload a photo in a Facebook album.
Also with small changes you can use this code to post either to your Facebook Profile or your Fan Pages or Business Pages.
Detailed instructions below are provided in this post.
The following PHP script can be invoked from command line (or Cron) or from your browser.
From the above code, it is obvious that
- you have to use Facebook PHP SDK (line 4)
- you have to create a Facebook app in order to use Facebook API (line 9,10)
- you have to get an Access Token, so your app can be on your post behalf (line 17)
- you have to renew Access Token after expiration (about 60 days)
- you have to get your Facebook id, either for your Personal Profile or for your Fan Pages or Business Pages (line 29)
All these steps are described below. I use a demo app ( Demo_auto_post_php ) without hiding «sensitive» information like app secret access tokens etc. The reason is to make the whole process more understandable. Of course, all these «sensitive» data is not valid. This app ( Demo_auto_post_php ) created just for demo purposes and it has been deleted .
Get Facebook PHP SDK
Download Facebook PHP SDK (v.3.2.2 at the time of writing this article) from Github at You may use any library available (in PHP section) of https://github.com/facebook/facebook-php-sdk .
Actually, you need three files (under folder src )
- facebook.php (the main class)
- base_facebook.php (other classes)
- fb_ca_chain_bundle.crt (SSL certificate)
Get and put them in the same directory (for example facebook_php_sdk ).
Facebook PHP SDK (v.3.2.2) Вимога
How to Create a Facebook app
Step 1 — Go to https://developers.facebook.com/apps and sign in with your account. Then press the button «+Create new app». New app name is Demo_auto_post
Короткий опис статті: on facebook or on facebook Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation. facebook, graph api, php, social media, webdev, web development, cron, how-to, Pontikis, blog, pontikis.net blog, technology blog, web development, web design, howto, tutorials, tech news