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();

Related Articles

Related Questions

C# How to pass ulong argument to unit test via TestCase decorator

I have a unit test where I want to pass some arguments into the test method via the TestCase decorator. The argument I need...

Can google home turn on my smart tv?

If I purchase a Google hub and set up everything I need to use Google Home to turn my house into a smart house....

Can amazon smart plug work with google home?

I am looking to purchase some smart plugs and I see that there is an amazon branded smart plug. It is advertised to work...



Please enter your comment!
Please enter your name here

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

Latest Tools

Bitrate Converter

Below you will find a bitrate converter. This tool will allow you to enter a bitrate value, in one of many different formats and...

Aesthetic Text Generator

There are various ways to make your social media profile seem more unique, some of which are not as easy to implement as others....

Aspect Ratio Calculator For Images

Aspect ratio is the ratio between the height and width of an image. If you want to resize an image by 100 pixels, you...

Add Text To Image

Use this free tool to add text to an image. Simply select the image file that you want to overlay text onto and you...

JavaScript Multi-line String Builder

Javascript did not always support multi-line strings. If you attempted to create a string variable using quotes, putting a line break into the source...

GUID Generator

GUID generation is relatively simple. It is a system that is used to generate unique values without having to keep track of the IDs...

Latest Posts

Latest Questions