Automatically Post Content To Facebook Using PHP

There eventually comes a point where you are creating a large amount of posts and manually posting to Facebook is just too time consuming. It’s very easy to automate the process of posting to Facebook using PHP. I would suggest you add a custom Facebook title field to your submit form that will allow you to define a custom message for the post to Facebook. This way you can reference other things that you don’t want to use in the default title for the post. This tutorial will provide you with a function that will automate the process. There are a few steps that you will need to perform before this function will successfully work.

First you will need to create a Facebook app. This can be done very easily on the developers site. <<SITE URL >>. The app works as a way for your PPHP code to make a post to Facebook on your behalf. It’s nothing to worry about in terms of security since it’s your app. Once you have created an app you will have access to an AppId and an app secret. Both of these values are vital as you will need to place them into the code snippet below.

$config['appId'] = 'APP ID';
$config['secret'] = 'SECRET';

The next step is to get an access token. You can get an access token by giving your newly created app permission to post to either your Facebook profile or one of your like pages. Click here and select your app. Allow the app to manage posts and that’s it. You will now have an access token! Copy and paste the token into the code.

“access_token” => “Your access token”

The code below relies on the PHP API to function. You can download the PHP api here Place the API code in any directory of your choice and be sure to update the require function on the first line of the function.


You are only one final step away from completing this. The final step is to choose which page you want to post to. The variable $feedID is going to be the ID of the Facebook page that you want to post to. The fastest way to get this ID is to visit the page in your internet browser. In the URL bar there will be a numeric value. This is the page ID. When calling the function you can pass this ID and it will post to that page. You can post to multiple pages if you wish.

The variables passed to the function below are self explanatory. As I have explained above $feedID is the Id for the page that you want to post to.

function postToFacebook($message, $link, $picture, $name, $caption, $description, $feedID)
  // require Facebook PHP SDK
  // see:
  // initialize Facebook class using your own Facebook App credentials
  $config = array();
  $config['appId'] = 'APP ID';
  $config['secret'] = 'SECRET';
  $config['fileUpload'] = false; // optional
  $fb = new Facebook($config);
  // These are the values that will create the post on facebook
  $params = array(
    "access_token" => "Your access token", // get an access token here -
    "message" => $message,
    "link" => $link,
    "picture" => $picture,
    "name" => $name,
    "caption" => $caption,
    "description" => $description
  // Attempt to post the article to facebook. The catch will return any error messages, these messages are very easy to understand which makes debugging a charm.
    $ret = $fb->api('/'.$feedID.'/feed', 'POST', $params);
    return 'Post successfully published to Facebook!';
  catch(Exception $e) 
    return $e->getMessage();

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.