Using Facebook API to post articles from PHP

How to automatically post to facebook page using PHP

This one is a bit detailed tutorial that is assumed for a novice php programmer who is new to Facebook API

Step 1.
Create a developer account from developer.facebook.com. All you need is your facebook account and you can get a developer account from it.

Step 2.
Create a Facebook page. From your Facebook page, on the top right corner, click on the dropdown menu and hit on create page. Select the type of the page you would like to create and then you have it.

Now what you want to do is, to create an auto post to the facebook page you just created from PHP. Lets say you run a blog or product page and you want to post whenever there is new stuff to be posted to the facebook page automatically.

Step 3.

Download Facebook PHP API from https://github.com/facebook/php-graph-sdk. You can either clone it using git if you have git already or just download the zip file and extract it to the directory of your webserver.

Step 4.
Create the following Class you will be using to post to Facebook


 
/**
 * Script for posting the videos to FB automatically.
 * @author Kaleb
 */
define ('FACEBOOK_SDK_V5_SRC_DIR', __DIR__.'/php-graph-sdk/src/Facebook/');
require_once(__DIR__.'/php-graph-sdk/src/Facebook/autoload.php');

class FacebookPoster
{
    private $facebook;

    public function __construct(array $facebook_data) 
    {   
        $this->facebook = new Facebook\Facebook($facebook_data);
    }   

    /** 
     * Post to facebook page using the given values.
     *
     */
    public function post(array $post_data, $token)
    {   
        if (!empty($post_data)) {
            try {
                $post_response = $this->facebook->post('/me/feed', $post_data, $token);
            } catch (Facebook\Exceptions\FacebookSDKException $ex) {
                // $logger->log($ex->getMessage();
                echo "Error occurred " . $ex->getMessage();
            }   
        }   
    }   

}

Step 6.

To run the above class you will use the following


$facebook_data = [ 
    'app_id' => ‘APP_ID_GOES_HERE’,
    'app_secret' => ‘APP_SECRET_GOES_HERE’,
    'default_graph_version' => 'v2.2'
];

$facebook_poster = new FacebookPoster($facebook_data);

$post_data = [
    'link' => 'YOUR LINK YOU WANT ON FACEBOOK',
    'message' => 'YOUR MESSAGE YOU WANT ON THE POST'
];

$token =‘YOUR_TOKEN_GOES_HERE’;

//post it.
$facebook_poster->post($post_data, $token);

Don’t worry about the app_id, app_secret and token for now. Those are the things you will get from facecbook and plug them in later.

Step 7.
Now create an application in Facebook
For this, got to https://developers.facebook.com and on the right top corner you will get the dropdown and in there you will see create app. Follow the wizard and you will create the application.

Step 8.

Go to https://developers.facebook.com/apps/ to see the app, click on it and you will see the app_id and app_secret there. Those are what will be needed by step6.

Now you need to hook the application with the facebook page.

Step 9.
Go to https://developers.facebook.com/tools/explorer/ and click the app you created from Graph API explorer

Step 10.

Then select user token from the get token part. Select public page and manage page for permission.

Step 11.
From the Get Token, select the facebook app you have created earlier. You will get the popup to assign the permission and just follow the wizard.

Step 12.
Go to the facebook page you created and go to the settings, on the left you will see apps. Click on the apps and search for the app you created. You will get permission stuff and just complete that.

Step 13.
Go back to the explorer page https://developers.facebook.com/tools/explorer/ and select the application from the dropdown and also select the facebook page from Get Token. Now pick the token and that will be the last part to be inserted as token on Step 6.

Now you have everything to crank your engine. Let me know if you got problem on this.

Post to Facebook From app – Using PHP

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

Leave a Reply

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

*
*