Post on Facebook via API

It can be very useful to have a something posted on your Facebook Page automatically and her eI will show you how to do it the the Facebook PHP API.

Step 1: let’s App it

If you don’t have it yet, create a Facebook App here: https://developers.facebook.com/apps/

Step 2: Let’s install it

Install the Facebook SDK on your server/hosting: you can do it via Composer or you can install it manually by downloading it on Git Hub, unzipping it on your hosting and then include it in your script like in the example here:

define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');

Step 3: let’s start it

Add you App ID and your App Secret in your script:

$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxx',
'app_secret' => 'xxxxxxxxxx',
'default_graph_version' => 'v2.2',
]);

Step 4: Let’s token it

Get a non expiring Facebook token here

On the dropdown menu Application you will see preselect Graph Api Explorer, here please select your Facebook application, and then click on the “Get Token” button making sure that you select “Get User Access Token” and chose “publish_pages” and “manage_pages” permissions and then click the ‘Get Access Token’ button.

Now click “Get Token” again and select your Facebook page: you now have the Access Token which you need to convert into the non-expiring Access Token, and you can simply do this operation by replacing the value in the Url below and then submit it in your browser:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=appid&client_secret=appsecret&fb_exchange_token=accesstoken

You now have the token, please copy and paste it in a safe place, you will need it in few seconds!

Step 5: Let’s finish it

Here is the rest of the ode needed to do the Post:

//Post property to Facebook
$linkData = [
'link' => 'www.yoururl.com',
'message' => 'Your message here',
'place' => 'Page ID of a location associated with this post',
'tags' => 'Comma-separated list of user IDs of people tagged in this post'

];
$pageAccessToken ='yournonexpiringtoken';

try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();

for a full and details list and description of the parameters you can post on your page please refer to this link: https://developers.facebook.com/docs/graph-api/reference/v2.11/user/feed#publish.

Note: unfortunately is not possible to post custom image, Facebook removed this option to fight the Fake News issue, you can still attach a photo but is must be already part of your Album: more details in the link above:

Step 5: Let’s cron it!

We now have the script! Let’s fully automate it by setting up a cron job that will regularly run your script and post your content automatically! On cPanel, setting up a cron job is very easy, if you don;t know how to do it, please get in touch, I will help you.