How to Post in Facebook Group in PHP

The feed of posts (including status updates) and links published to this group.

Step 1: Download Facebook Sdk on: Facebook php SDK and Copy src folder.

Step 2: Create config.php file in src folder path src/config.php.

  1. Config.php  
  2. <?php  
  3.     $config['Redirect_url']     =   'http://www.yoursitename.com/?fbTrue=true';  // Your Domain  /?fbTrue=true is required.  
  4.     $config['APP_ID']           =   '****************'// Your Facebook App ID  
  5.     $config['APP_SECRET_KEY']   =   '****************'// Your Facebook App Secret Key  
  6. ?>  
After this fill redirect url, facebook app id and facebook app secret key.

Step 3: After complete step 2 then create index.php in root folder.

Index.php

  1. <?php  
  2.     session_start();  
  3.     include('src/config.php');  
  4.     include('src/facebook.php');  
  5.   
  6.     $facebook = new Facebook(array(  
  7.                   'appId'  => $config['APP_ID'],       
  8.                   'secret' => $config['APP_SECRET_KEY'],  
  9.                   'cookie' => true  
  10.                 ));  
  11.     if(isset($_POST['status'])) {  
  12.         $groupId = $_POST['group'];  
  13.         $publishPost = $facebook->api('/'.$groupId.'/feed''post',  
  14.                                 array('access_token' => $_SESSION['token'],  
  15.                                 'message'=> 'Testing',  
  16.                                 'from' => $config['App_ID'],  
  17.                                 'to' => $groupId,  
  18.                                 'caption' => 'PHP Gang',  
  19.                                 'name' => 'PHP Gang',  
  20.                                 'link' => 'http://www.yoursitename.com/',// Your site Url  
  21.                                 'picture' => 'http://www.yoursitename.com/img/logo.png', // Your Image Link  
  22.                                 'description' => 'What is Loren ipsum ' // Your Description  
  23.                             ));  
  24.         $publishPost = $facebook->api('/'.$groupId.'/feed''post',  
  25.                                         array('access_token' => $_SESSION['token'],  
  26.                                                 'message'=>$_POST['status'] .'   via PHPGang.com Demo',  
  27.                                                 'from' => $config['App_ID']  
  28.                                     ));  
  29.         $message = 'Status updated.<br>';  
  30.         $graphUrlGroups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];  
  31.         $allGroups = json_decode(file_get_contents_curl($graphUrlGroups));    
  32.         $dropdownGroupList = "";  
  33.         for($i=0;$i<count($allGroups->data);$i++){  
  34.             $dropdownGroupList .= "<option value='".$allGroups->data[$i]->access_token."-".$allGroups->data[$i]->id."'>".$allGroups->data[$i]->name."</option>";  
  35.         }  
  36.         $contentStyle = '<style>  
  37.                             #status {  
  38.                                 width: 300px;  
  39.                                 height: 25px;  
  40.                                 font-size: 16px;  
  41.                             }  
  42.                         </style>'.$message.'  
  43.                         <form action="index.php" method="post">  
  44.                             Select Group you want to post status: <br>  
  45.                             <select name="group" id="status">'.$dropdownGroupList.'</select><br><br>  
  46.                             <input type="text" name="status" id="status" placeholder="" />  
  47.                             <input type="submit" value="Post On My Group!" style="padding: 5px;" />  
  48.                         <form>';  
  49.     }  
  50.     else if(isset($_GET['fbTrue'])) {  
  51.         $tokenUrl = "https://graph.facebook.com/v2.1/oauth/access_token?"  
  52.                     . "client_id=".$config['APP_ID']."&redirect_uri=" . urlencode($config['Redirect_url'])  
  53.                     . "&client_secret=".$config['APP_SECRET_KEY']."&code=" . $_GET['code'];  
  54.         $res = file_get_contents_curl($tokenUrl);      
  55.         $param = null;  
  56.         parse_str($res$param);  
  57.         $graphUrl = "https://graph.facebook.com/v2.1/me?access_token="   
  58.                     . $param['access_token'];  
  59.         $_SESSION['token'] = $param['access_token'];  
  60.         $user = json_decode(file_get_contents_curl($graphUrl));    
  61.         $graphUrlGroups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];  
  62.         $groups = json_decode(file_get_contents_curl($graphUrlGroups));    
  63.         $dropdownGroupList = "";  
  64.         for($i=0;$i<count($allGroups->data);$i++){  
  65.             $dropdownGroupList .= "<option value='".$allGroups->data[$i]->access_token."-".$allGroups->data[$i]->id."'>".$allGroups->data[$i]->name."</option>";  
  66.         }  
  67.         $contentStyle = '<style>  
  68.                             #status {  
  69.                                 width: 300px;  
  70.                                 height: 25px;  
  71.                                 font-size: 16px;  
  72.                             }  
  73.                         </style>'.$message.'  
  74.                         <form action="index.php" method="post">  
  75.                             Select Group you want to post status: <br>  
  76.                             <select name="group" id="status">'.$dropdownGroupList.'</select><br><br>  
  77.                             <input type="text" name="status" id="status" placeholder="" />  
  78.                             <input type="submit" value="Post On My Group!" style="padding: 5px;" />  
  79.                         <form>';  
  80.     } else {  
  81.         $contentMsg = 'Connect With     
  82.                         <a href="https://www.facebook.com/dialog/oauth?client_id='  
  83.                             .$config['APP_ID'].'&redirect_uri='  
  84.                             .$config['Redirect_url'].'&scope=email,user_about_me,publish_stream,publish_actions,user_groups">  
  85.                             <img src="./img/login.png" alt="Sign in with Facebook"/>  
  86.                         </a>';  
  87.     }  
  88.     echo $contentMsg;  
  89.     function file_get_contents_curl($url) {  
  90.         $c = curl_init();  
  91.         curl_setopt($c, CURLOPT_HEADER, 0);  
  92.         curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.  
  93.         curl_setopt($c, CURLOPT_URL, $url);  
  94.         $myData = curl_exec($c);  
  95.         curl_close($c);  
  96.         return $myData;  
  97.     }  
  98. ?>   
Ebook Download
View all
Learn
View all