How to update facebook status using php?

In this post, I'll be showing you how, you can write a php application to update your Facebook status. Before we start writing the application. Lets get prepared for it. Download the php sdk of Facebook from the link: https://github.com/facebook/facebook-php-sdk . Download the zip file and extract it. We need the src folder content in it, place it in the root directory of the application. i.e.  we have a directory fbapp created in which the src directory will be placed. Now we have the sdk, lets create a facebook application.
To create a Facebook app, you need to be logged into https://developers.facebook.com/apps . If not login now. And create an app. For this example, I am creating an app called Test.

And our fbapp directory is in test.com domain. So our application is accessible from http://test.com/fbapp. So lets add test.com to our website with Facebook application from the applications settings that you will have after creating the application which will look like the following screenshot and save the settings.
Here I have hidden the app id and app secret, which should not be disclosed by you too to others. Note the App Id and App Secret, we will be needing these very soon.
Now create a index.php file in the fbapp directory and place the following code in it.
<html>
<head>
       <title>Facebook dashboard</title>
</head>
<body>
<?php
include_once 'src/facebook.php';
$APP_ID='322215007880533';
$API_SECRET='f0379fb2a2dfb0fd420654a0d1474893';
$ACCESS_TOKEN='AAAElDYcWBVUBAGM36OPUvwEN9jogaF7ssOq2vS3bt4nwMxe8DCxzjTRF0DW9JeKyFXdzZCLrGE1CTORYS9Yx7a4Ju6Lw934bl3Ub0aZAIN968ZCrnQf';
$config = array(
      'appId'  => $APP_ID,
      'secret' => $API_SECRET,
      'cookie' => true,
    );
$facebook = new Facebook($config);
<?php
           $user=$facebook->getUser();
        if(!$user){
            $params = array(
                      'scope' => 'email,publish_stream',
                      'redirect_uri' => 'http://test.com/fbapp/'
                        );
            $loginUrl = $facebook->getLoginUrl($params);
            echo "<a href='".$loginUrl."'>"."Login"."</a>";
        }else{
?>
<form method=post action="" style="align:center;" >
Status:<textarea id=pageStatus name=status  ></textarea>            
<button type=submit >Post Status</button>                               
</form>
<?php
            $params = array( 'next' => 'http://opnchaudhary.me/fb/' );
            $logoutUrl=$facebook->getLogoutUrl($params);
            echo "<a href='".$logoutUrl."'>"."Logout"."</a>"
    }

if(isset($_POST['status'])){
        $attachment = array(
                'message' => $_POST['status'],
                'actions' => array(
                    array(
                        'name' => 'visit writephp',
                        'link' => 'http://writephp.blogspot.com/'
                    )
                )
            );
            $result = $facebook->api('/me/feed/', 'post', $attachment);
?>
</body>
</html>
Okey, you might have got what we are doing here. If not, we are going through the code. First 5 lines are normal html, that doesn't need any explanation. The next is the php block where we have included our facebook-php-sdk that we downloaded from git-hub.  Then we have App Id and App Secret that we obtained after creating the app. Now, what about the ACCESS_TOKEN. To get the access token go to: https://developers.facebook.com/tools/explorer and select your application the click on get Access token.
So, we have ACCES TOKEN too. Now, using those values, we create an instance of Facebook class using the code:
config = array(
      'appId'  => $APP_ID,
      'secret' => $API_SECRET,
      'cookie' => true,
    );
$facebook = new Facebook($config);
 Note: user your applications App Id, App Secret and Access Token. The above one won't work. They are just random values.

After that, we get the current user. If the user is logged in then we have the form displayed else we want the user to login. Here we are asking permission for email,publish_stream. Though email is not required you can omit it but you must have the publish stream to allow the application to publish on your behalf. Once you click login link, Facebook asks for the permission we have specified. Once the user is authenticated now, we can have the status written in the textarea. Then using the code:
$attachment = array(
                'message' => $_POST['status'],
                'actions' => array(
                    array(
                        'name' => 'visit writephp',
                        'link' => 'http://writephp.blogspot.com/'
                    )
                )
            );
            $result = $facebook->api('/me/feed/', 'post', $attachment);

The facebook status gets updated. its okey if you don't have the part  :
array(
                        'name' => 'writephp',
                        'link' => 'http://writephp.blogspot.com/'
                    )

This code makes a small link below your status as follows:
 Here in the image I have via WordPress Blog but don't worry its the name of the application I have used for you it will be test, since you are using test. Hope you love this.

Comments

  1. can i use it on facebook pages?

    ReplyDelete
  2. To use it for facebook pages, you need to do a small modification i.e. instead of
    $facebook->api('/me/feed/', 'post', $attachment);
    you should use use
    $PAGE_ID='07098709879080809'; //change the fb page id to your fb page.
    $facebook->api('/'.$PAGE_ID.'/feed/','post',$attachment);

    And it will work like charm.

    ReplyDelete
  3. how to insert image or video ?

    ReplyDelete
  4. What doesn't work? Can you elaborate? Like: What you did? What errors/problems you got? So that I can help you get done.

    ReplyDelete
  5. i have this error --> Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\htdocs\1Filter\detailedanalysis.php on line 17 .
    What should i put in ? and one more thing, where should i get the access token ? is it the same as user token ?

    ReplyDelete
  6. Noor Fahrahin you can go to the following link https://developers.facebook.com/tools/explorer to get your access token. And for the error you have mentioned I can only say once you show me your code snippet. As for now what I can say is you have an extra '<' character in your detailedanalysis.php file at line no 17 or possibly you have space between < and php or < and % or < and ?
    Hope that solves your problem. If not please do post your snippet file. So that I can figure out where your problem is.

    ReplyDelete
    Replies
    1. i just copy ur code to run but have the same problem with Noor Fahrahin

      Delete
  7. do you know how to create button add a location when updating the status?

    ReplyDelete
    Replies
    1. I haven't tried that yet. I'll see and get back to you.

      Delete

Post a Comment

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud