How to Post Into Facebook Group with PHP using Graph API
Hey guyz I received many requests from my readers to write tutorial on facebook Group status update so I am going to show you that how you can do update your joined groups. You can joined many groups and update your status on groups to promote your websites or your products on all groups and this is an easy way to promotion.
How to integrate:
To create Facebook application follow instruction on my previous post How to Login with Facebook Graph API in PHP
We need permissions publish_actions, user_groups to run this code on your website. You need to send request to facebook to review your application permission and allow your app for these permissions Read more.
Script contains two folders called src and images with PHP files.
src
– base_facebook.php // Class Get user Information.
– facebook.php // Class Get user Information.
– config.php // Configuration file.
images
index.php // Main index file.
PHP Code
Edit config.php
<?php $config['callback_url'] = 'CALL BACK URL/?fbTrue=true'; // /?fbTrue=true allow you to code process section. //Facebook configuration $config['App_ID'] = 'Your App ID'; $config['App_Secret'] = 'Your App Secret'; ?>
First of all we get current user’s joined groups list and display in drop down.
<?php $graph_url_groups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token']; $groups = json_decode(file_get_contents_curl($graph_url_groups)); // get all groups information from above url. $dropdown = ""; for($i=0;$i<count($groups->data);$i++) { $dropdown .= "<option value='".$groups->data[$i]->id."'>".$groups->data[$i]->name."</option>"; } ?>
Now we have all joined groups list in $dropdown variable.
Posting on Group is simply like we make post on facebook page in our previous tutorial How to post into a Facebook Page with PHP using Graph API below is the code for posting.
Publish Link with Image:
$publish = $facebook->api('/'.$group_id.'/feed', 'post', array('access_token' => $_SESSION['token'], 'message'=> 'Testing', 'from' => $config['App_ID'], 'to' => $group_id, 'caption' => 'PHP Gang', 'name' => 'PHP Gang', 'link' => 'https://www.phpgang.com/', 'picture' => 'https://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png', 'description' => 'Testing with PHPGang.com Demo' ));
Above code post on facebook group a link with image see attached image.
Publish text status only only:
$publish = $facebook->api('/'.$group_id.'/feed', 'post', array('access_token' => $_SESSION['token'],'message'=>$_POST['status'] .' via PHPGang.com Demo', 'from' => $config['App_ID'] ));
Above code share only text status on your selected group.
Here is my complete code to get user authentication and post status:
<?php session_start(); require 'src/config.php'; require 'src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => $config['App_ID'], 'secret' => $config['App_Secret'], 'cookie' => true )); if(isset($_POST['status'])) { $group_id = $_POST['group']; $publish = $facebook->api('/'.$group_id.'/feed', 'post', array('access_token' => $_SESSION['token'], 'message'=> 'Testing', 'from' => $config['App_ID'], 'to' => $group_id, 'caption' => 'PHP Gang', 'name' => 'PHP Gang', 'link' => 'https://www.phpgang.com/', 'picture' => 'https://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png', 'description' => 'Testing with PHPGang.com Demo' )); $publish = $facebook->api('/'.$group_id.'/feed', 'post', array('access_token' => $_SESSION['token'],'message'=>$_POST['status'] .' via PHPGang.com Demo', 'from' => $config['App_ID'] )); $message = 'Status updated.<br>'; $graph_url_groups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token']; $groups = json_decode(file_get_contents_curl($graph_url_groups)); // get all groups information from above url. $dropdown = ""; for($i=0;$i<count($groups->data);$i++) { $dropdown .= "<option value='".$groups->data[$i]->access_token."-".$groups->data[$i]->id."'>".$groups->data[$i]->name."</option>"; } $content = ' <style> #status { width: 357px; height: 28px; font-size: 15px; } </style> '.$message.' <form action="index.php" method="post"> Select Group on which you want to post status: <br><select name="group" id="status">'.$dropdown.'</select><br><br> <input type="text" name="status" id="status" placeholder="Write a comment...." /> <input type="submit" value="Post On My Group!" style="padding: 5px;" /> <form>'; } elseif(isset($_GET['fbTrue'])) { $token_url = "https://graph.facebook.com/v2.1/oauth/access_token?" . "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url']) . "&client_secret=".$config['App_Secret']."&code=" . $_GET['code']; $response = file_get_contents_curl($token_url); // get access token from url $params = null; parse_str($response, $params); $graph_url = "https://graph.facebook.com/v2.1/me?access_token=" . $params['access_token']; $_SESSION['token'] = $params['access_token']; $user = json_decode(file_get_contents_curl($graph_url)); // Get user information from given url $graph_url_groups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token']; $groups = json_decode(file_get_contents_curl($graph_url_groups)); // get all groups information from above url. $dropdown = ""; for($i=0;$i<count($groups->data);$i++) { $dropdown .= "<option value='".$groups->data[$i]->id."'>".$groups->data[$i]->name."</option>"; } $content = ' <style> #status { width: 357px; height: 28px; font-size: 15px; } </style> '.$message.' <form action="index.php" method="post"> Select Group on which you want to post status: <br><select name="group" id=status>'.$dropdown.'</select><br><br> <input type="text" name="status" id="status" placeholder="Write a comment...." /> <input type="submit" value="Post On My Group!" style="padding: 5px;" /> <form>'; } else { $content = '<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,publish_stream,publish_actions,user_groups"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>'; } echo $content; function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } ?>
Above code first of all authenticate you permission from facebook and then show a form with a drop down and a text box to select group and status to update.
In my script you can post on one group at 1 time but if you want to post on all groups in one click you can modify script and do it easily.
You can can make change and create a auto facebook group posting application.
That’s all for this tutorial hope you guyz like this tutorial please feel free to comment below.
Tutorial Categories:
Thank you for this tutorial.
Until now I succeeded to extract the feed from a Facebook Group and include it in a Android App using appyet.com. Now all the feeds display on the phone and I don’t have to keep facebook app open.
When they will upgrade their templates and allow php scripts to run I will implement this into same application to have a fully operational Facebook Group Application :D.
Thank you one more time, this resource is priceless.
but facebook feed needs login into facebook acc. and how can you login facebook into appyet app ??
I cannot download the source code. I’m already subscribed to the mailing list .But can’t download can you please email me a sourcecode.
i too. i cannot download code.
i need you help when i download code then its work fine only 1 issue is their can u suggest me how i can fetch group account to user login
now its working with single user onlt its get single user group
hello
anybody here
anybody can help me
code not working it’s not load group
Please send me errors.
thanks you so much , it’s works
Please help me for a trouble ?
It’s not show scope when user access apps.
sorry for my English
hello. i can’t download code. please help me
What is the problem you are facing”
Hello there,
I need to publish link on all my groups. I set sleep(10) after every post. I am getting below listed error.
What to do?
Fatal error: Uncaught FacebookApiException: Permissions error thrown in /home/storyreads/storyreads.com/fb/base_facebook.php on line 1325
Hello.
I did as in your tutorial but it does not work. Could you please help me. This is my error
“The parameter app_id is required”
I think that the problem is my facebook app, but I can not fix it. Please help me Thank you!
Hello Dont show my groups
can you tell me about auto post in all group ?
Sr plz help me. not show group name plz check this http://www.facebeokcomfblol.hostingsiteforfree.com/fbpost/index.php
Please send specific errors so i can help you try to debug.
i add this items in app plz tell me about this.sorry for english mistak.
Sr agr ap kahty hu tu username and password b dy dyta hu par plz sr mari help karo
The redirect_uri URL must be absolute im having these error
What url you are sending?
Sr this is error plz solve my problem plz sr.first time join this site plz help me
You need to submit your app first for review for these permission once your app approved then you can use it. Read this: https://developers.facebook.com/docs/apps/review/
Sr kun kun sy items select karny is group posting ky lye.
demo is not working. i can’t choose Group
The redirect_uri URL must be absolute im having these error
Please Help me in this.
I got the error “The parameter app_id is required”. I have the app_id nad secret number as well, but, there´s something wrong… have you testing the script latelly? Have you an update or something?
Thks a lot from Brazil!
I got the error “The parameter app_id is required”. I have the app_id nad secret number as well, but, there´s something wrong… have you testing the script latelly? Have you an update or something?
Thks a lot from Brazil!
I need this to be implemented using javascript only.Is it possible?If yes do help bro.
Regards
i can’t sub and download?
Fatal error: Uncaught Error: Call to a member function api() on null in D:0.Serverwww0.WebTestingFacebook.Api.Groupefacebook.php:14 Stack trace: #0 D:0.Serverwww0.WebTestingFacebook.Api.Groupeindex.php(5): require() #1 {main} thrown in
Is this manual actual for nowadays?
Hi,
The demo is showing the error. I think the api has changed now. Will you please write an article about the new API of the Facebook sharing in the groups?