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

Can We Set Up MFA for Accessing SMB Shares?

My boss emailed me asking if there's a way to enforce MFA when directly connecting to our server drives, since he can currently access...

Unexpected Searches in My Chrome History – What’s Going On?

Hey folks, I just got a new MacBook M4 and I noticed something strange: there are searches appearing in my Google Chrome history that...

What AM5 Motherboards Do You Recommend for My New PC Build?

Hey everyone! I'm in the process of building a new PC, and I'm on the lookout for an AM5 motherboard that meets a few...

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

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions