Reader Level:
ARTICLE

Post on FaceBook Wall Using PHP Script

On March 06, 2013 Articles | PHP
In this article I explain how to post a messages link to a user's Facebook wall using PHP script.
  • 1
  • 0
  • 80566


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 

Sharad Gupta

Hi I am a Software developer working with different Microsoft technologies like ASP.NET 3.5/4.0, AJAX, JavaScript, J Query, Ms-access, SOL Server 2005/2008/2014,ADO.NET,Typescript also enamoured to le...

  • 36rank
  • 1M readers
COMMENT USING

Trending up