If you look for alternative way to integrate Facebook into your website, you should try the Facebook API. With the Graph API and the Facebook Login feature it’s possible to authenticate users in your web application and upload or post content to a user’s timeline. This Facebook API tutorial is an example how to post a link to the user’s timeline. Check the Facebook API tutorial demo page to see this PHP code in action.

Setting up a Facebook application

Before you’re able to start, it’s necessary to have a Facebook account and a web hosting account. To access the Facebook API you need an App ID and secret. Register your Facebook App via the FaceBook Developer Section. From the top menu select “Apps => Create a New App” to open the pop-up for your app details. Enter the required fields like in the screenshot.

Facebook API PHP Tutorial

After the app is created, click on “Settings” in the left menu. Enter the domain were your web application is hosted or better click the “Add Platform button -> Website” and enter the web application URL (directory) where your web application is located. Keep the browser window open to have access to the App ID and secret on a later moment.

The Facebook SDK for PHP v4.0

In this Facebook API tutorial I use the PHP library or better the Facebook SDK v4.0 for PHP v5.4. You can install the SDK using Composer or download the PHP SDK and upload the files like I did. Extract the ZIP file, rename the directory to “facebook-php-sdk” and upload the directory to your web host (create a new directory first and match the URL you’ve used for your Facebook App).

Facebook API PHP example

Create inside your new directory a new PHP file and add the following code:

Be sure that the path to the autoload.php is correct otherwise the missing PHP libraries will return (only) some nasty error messages. Enter also the Facebook App ID and secret which you can copy from the Facebook App page you’ve created before. The redirect login URL is the URL from your PHP file.

Authentication and authorization

To post a link to a user’s Facebook timeline, the application need an authorization. This is actually a complex function, add this code below the others in your PHP file.

After the application is initiated the helper object is created. This object is used later if a valid Facebook session is messing. The first IF statement is used to check if there is already a valid (PHP) session for the current visitor or user. If the session isn’t valid or empty the helper object is used to create a new session from the login / redirect.

Requests to the Graph API

At the moment that the session is valid, the script is able to post the link to the user’s Facebook timeline. Add this snippet below the other PHP code.

This part of the script does the following: If $session exists, the script stores the object inside a PHP session variable and a new Facebook session object is created. Next we do a request to Facebook that will post the link including the message. Now I create a GraphObject from the response and the script outputs the data to the browser. I use the Facebook message ID for a second request:

This request is used to check if the Facebook message really exists. The GET request is almost the same as the previous POST request. Last but not least we take a look on the code which creates the login URL (place this code below the other).

If the session doesn’t exists, the script returns a login URL which is created from the helper object. Give some attention to the action value I’ve passed to the getLoginUrl method, this value is necessary to post to the users timeline.

That’s all for this tutorial, you can download the tutorial code and check also the code comments for a better understanding. If you understand how it works, you can use this function (a bit modified) as a social feature for the comments form on your WordPress website.

Resources: Facebook PHP SDK Updated to v4.0.0