Post on FaceBook Wall Using PHP Script


Introduction

This article describes how to post content (status, image, URL) to a user's Facebook wall using a PHP script. To do that, use the following procedure.

Step 1

Go to https://developers.facebook.com/apps, and click on "Create New Application" which is at the right site. 

clicl-on-create-application.jpg

After clicking on the image given below, the "dialog box" has been opened, you can provide the name of your application then click on the "Continue" button.


create-application.gif
Step 2

After clicking on the Continue button, the new Captcha dialog box will be opened. Fill it in and click on the "Continue" button.

fill-security-words.gif 

Step 3

After clicking on the Continue button, your app id and secret has been generated, as you will see in the following image:

after-filling-security-code-you-appis-and-secret.jpg 

Step 4

After Step 3, enter the name of the Website you want to share messages and links of. You can also write the localhost (the id of your application that runs on the local server) with your PHP application name. The image given below shows how to do that. Then click on the "Save changes" button.

write-your-wesite-turl.jpg

Step 5

After the completion of all these steps, download:

facebook-php-sdk-master.

facebook-php-sdk-master-link.jpg

It downloads as a zip file, extract it into your website root. Suppose I am using the "wamp" server, so I extract it into the www root.

php-sdk-master-extraction.gif

Step 6

Now let's start working with the PHP script.

<html>

 <body>

<?php

    //facebook application configuration

    $fbconfig['appid' ] = "Write app id here";

    $fbconfig['secret'] = "Write secrete here";

    try{

        include_once ('.\facebook-php-sdk-master\src\facebook.php');

    }

    catch(Exception $o){

        print_r($o);

    }

    $facebook = new Facebook(array(

      'appId'  => $fbconfig['appid'],

      'secret' => $fbconfig['secret'],

      'cookie' => true,

    ));

 

    $user       = $facebook->getUser();

    $loginUrl   = $facebook->getLoginUrl(

            array(

                'scope'         => 'email'

            )

    );

 

    if ($user) {

      try {

        $user_profile = $facebook->api('/me');

        $user_friends = $facebook->api('/me/friends');

        $access_token = $facebook->getAccessToken();

      } catch (FacebookApiException $e) {

       // d($e);

        $user = null;

      }

    }

    if (!$user) {

        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";

        exit;

    }

 

 $args = array(

    'message'   => 'My First Fbapplication With PHP script!',

    'link'      => 'http://www.c-sharpcorner.com/',

    'caption'   => 'Latest toorials!'

);

$post_id = $facebook->api("/me/feed", "post", $args);

?>

</body>

</html>

Output

when you only use

$args = array(
    'message'   => 'My First Fbapplication With PHP script!',
);


fb-wall-post-message.jpg

When you use:

$args = array(
     'message'   => 'My First Fbapplication With PHP script!',

     'link'      => 'http://www.c-sharpcorner.com/',

     'caption'   => 'Latest toorials!'
 );

fb-application.jpg 

Up Next
    Ebook Download
    View all
    Learn
    View all