Blog Home Dudermang

Automatically post to a Facebook page using the Facebook PHP API (v5)

by cfunk

Posted on 2016-10-04 20:20:53



The Facebook API lets you post to pages you own or have admin rights. In the article below I’m going to explain how I did this, using version 5 of the Facebook SDK for PHP.

The idea was to write a script which automatically posted new blogposts to Facebook . So here’s how it went:

1. Create a Facebook 'App':

First you need to create a Facebook App. (required in order to use the API) Sign in to your Facebook Developers account and click the ‘Add a New App’ link as shown below:

Create A Facebook App

Enter an App name and click ‘create’. Under ‘settings’, you’ll need to add a ‘website platform’ and enter the domain where this script will sit. For security, it’s also worth adding the IP address of the server where your site sits in the ‘Server IP Whitelist’ field. You do so under ‘advanced settings’. I had to add my ipv6 public ip address of my laptop to get it to work while testing but my webserver's ipv4 address worked fine in production.

2. Install the Facebook PHP SDK:

I want you to create a ‘facebook’ folder in the root directory of your site. Then create a blank ‘index.php’ file within your ‘facebook’ folder. Next up, you need to include the Facebook SDK for PHP.

Facebook recommends you include the PHP SDK in your project using Composer, but I am not familiar with it so I did it manually. In this example we are manually including the SDK, so you will want to download the SDK.

Unzip the files and put the ‘src’ folder in your ‘facebook’ folder. Then include the SDK in your index.php file:

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

3. Enter your App's settings:

Next, you need to enter your ‘App ID’ and ‘App secret’ keys into the code below. You can get your App’s ‘App ID’ and ‘App secret’ under the ‘dashboard’ tab:

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

4. Get a non-expiring Access Token for your App:

Next, if you want to automatically post content to your Facebook page, you’re going to need to get a non-expiring Access Token for your page. visit the Facebook Graph API Explorer page. You’ll see an ‘Application’ button in the top right as show below:

Create A Facebook App

As you can see, currently this is set to ‘Graph API Explorer' select your Facebook App instead.
Now click the ‘get token’ button which you can also see in the image above.
Select ‘Get User Access Token’ and click the ‘extended permissions’ link.
Tick the ‘publish_pages’ and ‘manage_pages’ permissions.
Then click the ‘Get Access Token’ button.
Now click the ‘get token’ button again and select your Facebook page. You now have your Access Token which we need to convert into the non-expiring Access Token. To do so, pass your App ID, App Secret and Access Token into the following URL:

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

Now copy the token provided.

5. The code that does the 'posting':

The code that actually posts the content to the Facebook page.

//Post property to Facebook  $linkData = [   'link' => 'www.yoururl.com',   'message' => 'Your message here'  ];  $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();

There you go, if you now run this file it will post your content to your Facebook page.

Note: You will see the post appear on the Facebook page, but no-one else will as you have not set your App ‘live’! To do so, go back to your Facebook Developers account and select your App. Under the ‘Status & Review’ tab tick the checkbox at the top of the page next to the text ‘Do you want to make this app and all its live features available to the general public?’.

6. The automation:

Now all I needed was the automation. To achieve this all I needed to do was set up a cron job that ran the index.php file every x amount of hours and there was the automation.
crontab -e

Back

Keywords

Automatically post to a Facebook page using the Facebook PHP API (v5)