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 https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname&version=v2.2 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 https://github.com/facebookarchive/facebook-php-sdk. 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.

require_once($_DOCUMENT['ROOT']."/APIs/facebook.php");

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.

<?php
function postToFacebook($message, $link, $picture, $name, $caption, $description, $feedID)
{
  // require Facebook PHP SDK
  // see: https://developers.facebook.com/docs/php/gettingstarted/
  require_once($_DOCUMENT['ROOT']."/APIs/facebook.php");
   
  // 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 - https://developers.facebook.com/docs/facebook-login/access-tokens/
    "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.
  try 
  {
    $ret = $fb->api('/'.$feedID.'/feed', 'POST', $params);
    return 'Post successfully published to Facebook!';
  } 
  catch(Exception $e) 
  {
    return $e->getMessage();
  }
}
?>

Related Articles

Related Questions

What’s the Best Way to Clean Up My C Drive on an Old Laptop?

I recently inherited an old laptop from my sister, and I noticed that the C drive is almost full. Are there some effective methods...

Why Is My Binary Search Tree Code Not Working?

Hey everyone, I'm struggling to understand why my binary search tree code isn't functioning correctly. I've tried posting the errors I encountered, but the...

What’s the best way to remove super glue from my laptop cover?

Hey everyone! So I accidentally got super glue on the cover of my laptop (it's not on the screen, thank goodness). English isn't my...

1 COMMENT

LEAVE A REPLY

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

xAI Grok Token Calculator

This tool is a simple xAI Grok token calculator that helps you estimate the number of tokens your input text might consume when working...

DeepSeek Token Calculator

This tool is a straightforward DeepSeek token calculator, created to help you estimate how many tokens your text may use when working with DeepSeek...

Google Gemini Token Calculator

This tool is a simple Google Gemini token calculator, designed to help you estimate how many tokens your text might use with models in...

Meta LLaMA Token Calculator

This tool is a basic Meta LLaMA token calculator, designed to give you a fast estimate of how many tokens your input text might...

Anthropic Claude AI Token Calculator

This tool is a simple Claude token calculator, built to help you quickly estimate token usage when working with Anthropic’s Claude models. It’s an...

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

Latest Posts

Latest Questions