Friday, January 13, 2017

How to post silently or offline content post to user wall facebook[Solved] ?

Hi All,

I would like to share this post for help some one that looking for silently post their content to users wall . so it is very easy to integrate Facebook Graph Api using PHP API in  your website. I do not want to waste your time it small step which you need to follow bellow .


Step-1->  Download Facebook SDK from Facebook PHP SDK 
Step-2-> Create a your project folder paste the SDK into c:\xampp\fb\directory
Step-3-> Create a new file getAccessToken.php
Folder structure 
c:\xampp\fb\
c:\xampp\fb\src\Facebook

 1. autoload.php
##########START#####################################################
<?php
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  throw new Exception('The Facebook SDK v4 requires PHP version 5.4 or higher.');
}

spl_autoload_register(function ($class)
{
  // project-specific namespace prefix
  $prefix = 'Facebook\\';

  // base directory for the namespace prefix
  $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';

  // does the class use the namespace prefix?
  $len = strlen($prefix);
  if (strncmp($prefix, $class, $len) !== 0) {
    // no, move to the next registered autoloader
    return;
  }

  // get the relative class name
  $relative_class = substr($class, $len);

  // replace the namespace prefix with the base directory, replace namespace
  // separators with directory separators in the relative class name, append
  // with .php
  $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

  // if the file exists, require it
  if (file_exists($file)) {
    require $file;
  }
});
##########END###########################################################


2. getAccessToken.php
#########START##############getAccessToken.php################################
<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;

// start session

// inti app with app id and secret
FacebookSession::setDefaultApplication( 'APPid','SecretId' );

// login helper with redirect_uri

    $helper = new FacebookRedirectLoginHelper('http://127.0.1/fb/postwall.php' );

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if (isset($session))
{   
    $hostName    =        'http://'.$_SERVER['HTTP_HOST'];
    $getAccesstoken        =        $session->getToken();
}
 ###################INSERT TOKEN INTO DB###############################
 INSET INTO TABLE(USER_ID,FBACCESSTOKEN)VALUE('1001',$getAccesstoken);
###################INSERT TOKEN INTO DB################################
   

########################END################################### 


###########CREATE FILE#######silentPostWall.php###############################

<?php

    require_once('autoload.php');
    use Facebook\FacebookSession;  
    use Facebook\FacebookRequest;


#######SET CONFIGURATION FACEBOOK####################
                   FacebookSession::setDefaultApplication( 'AAPID','SCRETEKEY' ); 

###GET USER ACCESSTOKEN FROM DATABASE WHICH YOU STORED##########
                     $facebookAccesstokenFromdb    =    TABLE('td_usersAccessToken','1001');
                      

                        if(!empty($facebookAccesstokenCode) && $facebookAccesstokenCode!='NULL')
                        {
                            $pageSession = new FacebookSession($
facebookAccesstokenFromdb);
                            $request = (new FacebookRequest($pageSession, 'POST', '/me/feed', array(
                            'name' =>'Title',
                            'caption' => 'Title Caption',
                            'link' => 'URL',
                            'message' =>  'Des'
                            )))->execute();
                        }


###########END FILE#######silentPostWall.php###############################

 Finally you have done! 

Note : You need get permission approve "PUBLISH_ACTION" From Facebook to publish user wall.before you get approval you can post the content to developer user wall only.

Step1-> Create a screen cast video and give the instruction where your website user can enter their post  custom.



 Hope it will help you.. :)

No comments:

Post a Comment