Array

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

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

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

Markdown To Html Converter

Welcome to our web-based tool designed to make your life easier by converting Markdown to HTML in a matter of seconds. Our user-friendly interface...

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Latest Posts

Latest Questions