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.
- Config.php
- <?php
- $config['Redirect_url'] = 'http://www.yoursitename.com/?fbTrue=true'; // Your Domain /?fbTrue=true is required.
- $config['APP_ID'] = '****************';
- $config['APP_SECRET_KEY'] = '****************';
- ?>
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
- <?php
- session_start();
- include('src/config.php');
- include('src/facebook.php');
-
- $facebook = new Facebook(array(
- 'appId' => $config['APP_ID'],
- 'secret' => $config['APP_SECRET_KEY'],
- 'cookie' => true
- ));
- if(isset($_POST['status'])) {
- $groupId = $_POST['group'];
- $publishPost = $facebook->api('/'.$groupId.'/feed', 'post',
- array('access_token' => $_SESSION['token'],
- 'message'=> 'Testing',
- 'from' => $config['App_ID'],
- 'to' => $groupId,
- 'caption' => 'PHP Gang',
- 'name' => 'PHP Gang',
- 'link' => 'http://www.yoursitename.com/',// Your site Url
- 'picture' => 'http://www.yoursitename.com/img/logo.png', // Your Image Link
- 'description' => 'What is Loren ipsum '
- ));
- $publishPost = $facebook->api('/'.$groupId.'/feed', 'post',
- array('access_token' => $_SESSION['token'],
- 'message'=>$_POST['status'] .' via PHPGang.com Demo',
- 'from' => $config['App_ID']
- ));
- $message = 'Status updated.<br>';
- $graphUrlGroups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];
- $allGroups = json_decode(file_get_contents_curl($graphUrlGroups));
- $dropdownGroupList = "";
- for($i=0;$i<count($allGroups->data);$i++){
- $dropdownGroupList .= "<option value='".$allGroups->data[$i]->access_token."-".$allGroups->data[$i]->id."'>".$allGroups->data[$i]->name."</option>";
- }
- $contentStyle = '<style>
- #status {
- width: 300px;
- height: 25px;
- font-size: 16px;
- }
- </style>'.$message.'
- <form action="index.php" method="post">
- Select Group you want to post status: <br>
- <select name="group" id="status">'.$dropdownGroupList.'</select><br><br>
- <input type="text" name="status" id="status" placeholder="" />
- <input type="submit" value="Post On My Group!" style="padding: 5px;" />
- <form>';
- }
- else if(isset($_GET['fbTrue'])) {
- $tokenUrl = "https://graph.facebook.com/v2.1/oauth/access_token?"
- . "client_id=".$config['APP_ID']."&redirect_uri=" . urlencode($config['Redirect_url'])
- . "&client_secret=".$config['APP_SECRET_KEY']."&code=" . $_GET['code'];
- $res = file_get_contents_curl($tokenUrl);
- $param = null;
- parse_str($res, $param);
- $graphUrl = "https://graph.facebook.com/v2.1/me?access_token="
- . $param['access_token'];
- $_SESSION['token'] = $param['access_token'];
- $user = json_decode(file_get_contents_curl($graphUrl));
- $graphUrlGroups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];
- $groups = json_decode(file_get_contents_curl($graphUrlGroups));
- $dropdownGroupList = "";
- for($i=0;$i<count($allGroups->data);$i++){
- $dropdownGroupList .= "<option value='".$allGroups->data[$i]->access_token."-".$allGroups->data[$i]->id."'>".$allGroups->data[$i]->name."</option>";
- }
- $contentStyle = '<style>
- #status {
- width: 300px;
- height: 25px;
- font-size: 16px;
- }
- </style>'.$message.'
- <form action="index.php" method="post">
- Select Group you want to post status: <br>
- <select name="group" id="status">'.$dropdownGroupList.'</select><br><br>
- <input type="text" name="status" id="status" placeholder="" />
- <input type="submit" value="Post On My Group!" style="padding: 5px;" />
- <form>';
- } else {
- $contentMsg = 'Connect With
- <a href="https:
- .$config['APP_ID'].'&redirect_uri='
- .$config['Redirect_url'].'&scope=email,user_about_me,publish_stream,publish_actions,user_groups">
- <img src="./img/login.png" alt="Sign in with Facebook"/>
- </a>';
- }
- echo $contentMsg;
- function file_get_contents_curl($url) {
- $c = curl_init();
- curl_setopt($c, CURLOPT_HEADER, 0);
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($c, CURLOPT_URL, $url);
- $myData = curl_exec($c);
- curl_close($c);
- return $myData;
- }
- ?>