facebook page post from PHP

facebook auto post using php

facebook page post from PHP

How to autopost to facebook page using php

Facebook auto post using php, hmm.., I have a post on this topic a while ago, this one.

This post answers your questions of how to post to facebook page using php.

Though some of the initial steps do still hold, that is no more the case today. In order to combat the fake news and other improper usage of the platform, facebook has come up with different approach.

This time it requires a user involvement.

facebook auto post using php

Still most of the steps from the above link I mentioned works, but there are other major things you have to have. The first is claiming authority of the website you are about to post to, or at least having the right access to it. And second is, to grant permission to the facebook app you have created.

Step by step to post articles to facebook page from PHP

  1. Follow the steps on this one to create facebook developer account and facebook application.

Before we proceed to the next step, remember the two things we need, website ownership and granting permission to the app to post to your website.

As to the ownership, follow this post and verify you own the website.

Once you verify the ownership, the next will be granting the permission to the application. For this, your, the owner of the page, is required. Basically you need to have a token to be used from your script.

By now you have the facebook SDK to run your auto posting to facebook page

Create a file named init.php and add the following content to it:

 
<?php
session_start();
date_default_timezone_set('America/New_York'); 

define ('FACEBOOK_SDK_V5_SRC_DIR', __DIR__.'/php-graph-sdk5/src/Facebook/');
require_once(__DIR__.'/php-graph-sdk/src/Facebook/autoload.php');

$login_url = 'http://localhost'; //use your url here 

$fb = new Facebook\Facebook([
'app_id' => 'YOUR APP ID HERE',
'app_secret' => 'YOUR APP SECRET HERE',
'default_graph_version' => 'v3.1',
]);

On the above code, add the app_id and app_secret you get from facebook.

Mind you, you have to have the facebook SDK on the same folder as init.php just for this work. Or if you have it somewhere else, make sure the path is properly mentioned.

Once you have this, create another php file called login.php and add the following content to it:


<?php
include ("init.php");

$helper = $fb->getRedirectLoginHelper();

$permissions = ['manage_pages','publish_pages'];
$loginUrl = $helper->getLoginUrl($login_url.'/fbs/fb-callback.php', $permissions);

echo 'Log in with Facebook!';

This is the biggest part of post automatically to Facebook. This is the second part I mentioned earlier – the granting role to app part.

You can see on the code there are two roles specifically mentioned, manage_pages and publish_pages. These are important for app to post to the website.

If all went good, go ahead and access login.php – http://localhost/login.php or the URL u are using. That will ask you to log into facebook, if you haven’t already, and will give you a popup to grant permission to the app.

When you grant the app, Facebook will return list of tokens to each page you own that the app will have access to.

Almost done, grab the token from the response you got and use it on the actual posting code you have prepared.

 

Now you know how to facebook auto post using php. If you have any questions or something that is not working for you, ask in the comment section and I will get back to you.

connect to sftp from php

$_POST vs $HTTP_RAW_POST_DATA vs php://input and enctype

how to deploy symfony application to the production server

Deploying Symfony on Production Server

Run single phpunit test

composer install-update killed on vagrant machine

Leave a Reply

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

*
*