bendiksens.net
Login
Automatically post to a Facebook page using the Facebook PHP API (v5)

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

Posted: 18.12.2016 00:47 by Mikael


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


1. Create a Facebook ’App’:

Firstly you need to create a Facebook App, which is 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 your 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’.

2. Install the Facebook PHP SDK:

In this example I’ll assume your code will sit in a ‘facebook’ folder in the root directory of your site. Firstly, create a blank ‘index.php’ file within your ‘facebook’ folder. Next up, you need to include the Facebook SDK for PHP.

Facebook recommend you include the PHP SDK in your project using Composer, but you can also manually do so. In this example we’ll manually include the SDK, so you firstly need 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 up, 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 FacebookFacebook([ ’app_id’ => ’xxxxxxxxxx’, ’app_secret’ => ’xxxxxxxxxx’, ’default_graph_version’ => ’v2.2’,]);

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

Next up, if you want to automatically post content to your Facebook page like I needed to…you’re going to need to get a non-expiring Access Token for your page. Firstly, 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’:

Next up, 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(FacebookExceptionsFacebookResponseException $e) {
    echo ’Graph returned an error: ’.$e->getMessage();
    exit;
}
catch(FacebookExceptionsFacebookSDKException $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?’.