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