How to Post Into Facebook Group with PHP using Graph API

How to Post Into Facebook Group with PHP using Graph API

In this Post We Will Explain About is How to Post Into Facebook Group with PHP using Graph API With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Graph API Reference Group /group Example

In this post we will show you Best way to implement Post on a Facebook Group using 2.0+ API, PHP SDK, hear for How to Post Into Facebook Group with PHP using Graph API with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Post to Facebook Group Wall PHP API Example

To Make Facebook web-application follow some step by step instruction on my simple previous data post How to Login with Facebook Graph API in PHP

We need simple permissions data like as a publish_actions and user_groups to simple run this source code on your new website. We need to simple send request to data facebook to data review your web application data permission and simple allow your web-app for these data permissions permission Read or more/p>

Script contains two folders called src and images with PHP files.

src
– base_facebook.php //Pakainfo.com Class Get user Information.
– facebook.php //Pakainfo.com Class Get user Information.
– config.php //Pakainfo.com Configuration file.
images
index.php //Pakainfo.com 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 LIVE FACEBOOK DEV App ID';
$config['App_Secret']  =   'Your LIVE FACEBOOK DEV App Secret'; 

?>

First of all simple we get current facebook user’s joined all groups simple list and see in drop down box.

Read Also:  How to Insert Data Into MySQL Database Table Using PHP?

<?php
$graph_url_groups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];
    $groups = json_decode(live_dynemic_get_data_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>";
    }
?>

And then we have all simple joined groups list in $dropdown variable.

Posting on any facebook Group is simply like we make post on facebook page in our data previous Article How to simple post into a data Facebook Page with simple PHP using Live Graph API below is the source code for posting.

Publish Link with Image:

$publish = $facebook->api('/'.$live_group_id.'/feed', 'post',
            array('access_token' => $_SESSION['token'],
            'message'=> 'Live Data Testing',
            'from' => $config['App_ID'],
            'to' => $live_group_id,
            'caption' => 'PHP live24u',
            'name' => 'PHP live24u',
            'link' => 'https://www.Pakainfo.com/',
            'picture' => 'https://www.Pakainfo.com/wp-Data_content/themes/live24u_v2/img/logo.png',
            'description' => 'Live Data Testing with Pakainfo.com Demo'
            ));

Above code post on facebook group a link with image see attached image.

Publish text status only only:

$publish = $facebook->api('/'.$live_group_id.'/feed', 'post',
        array('access_token' => $_SESSION['token'],'message'=>$_POST['status'] .'   via Pakainfo.com Demo',
        'from' => $config['App_ID']
        ));

Above simple source code share data only text some status on your id means selected group.

Here is my simple complete source code data to get user simple user authentication and data post status:

<?php
session_start();
require 'src/config.php';
require 'src/facebook.php';
// Create our web facebook Application data instance
$facebook = new Facebook(array(
  'appId'  => $config['App_ID'],
  'secret' => $config['App_Secret'],
  'cookie' => true
));

if(isset($_POST['status']))
{
    $live_group_id = $_POST['group'];
    
    $publish = $facebook->api('/'.$live_group_id.'/feed', 'post',
            array('access_token' => $_SESSION['token'],
            'message'=> 'Live Data Testing',
            'from' => $config['App_ID'],
            'to' => $live_group_id,
            'caption' => 'PHP live24u',
            'name' => 'PHP live24u',
            'link' => 'https://www.Pakainfo.com/',
            'picture' => 'https://www.Pakainfo.com/wp-Data_content/themes/live24u_v2/img/logo.png',
            'description' => 'Live Data Testing with Pakainfo.com Demo'
            ));
        $publish = $facebook->api('/'.$live_group_id.'/feed', 'post',
        array('access_token' => $_SESSION['token'],'message'=>$_POST['status'] .'   via Pakainfo.com Demo',
        'from' => $config['App_ID']
        ));
        $message = 'here success Status updated.<br>';
        $graph_url_groups = "https://graph.facebook.com/v2.1/me/groups?access_token=".$_SESSION['token'];

    $groups = json_decode(live_dynemic_get_data_curl($graph_url_groups));
   
    $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>";
    }
    
    $Data_content = '
    <style>
    #status
    {
        width: 452px;
        height: 30px;
        font-size: 15px;
    }
    </style>
    '.$message.'
    <form action="index.php" method="post">
    Select any Group on which We want to post status: <br><select name="group" id="status">'.$dropdown.'</select><br><br>
    <input type="text" name="status" id="status" placeholder="Write a post comment...." />
    <input type="submit" value="Post On My Group Good Luck!" style="padding: 10px;" />
    <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 = live_dynemic_get_data_curl($token_url);   // get access token from url
    $live_param = null;
    parse_str($response, $live_param);

    $graph_url = "https://graph.facebook.com/v2.1/me?access_token=" 
        . $live_param['access_token'];
        $_SESSION['token'] = $live_param['access_token'];
    $user = json_decode(live_dynemic_get_data_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(live_dynemic_get_data_curl($graph_url_groups)); // simple get all data groups information data from above url.
   
    
    $dropdown = "";
    for($i=0;$i<count($groups->data);$i++)
    {
        $dropdown .= "<option value='".$groups->data[$i]->id."'>".$groups->data[$i]->name."</option>";
    }
    
    $Data_content = '
    <style>
    #status
    {
        width: 357px;
        height: 28px;
        font-size: 15px;
    }
    </style>
    '.$message.'
    <form action="index.php" method="post">
    Select any live Group on which We want to simple 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="Good Luck Post On My Group!" style="padding: 5px;" />
    <form>';
}
else
{
    $Data_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 $Data_content; 

function live_dynemic_get_data_curl($url) {
    $my_ch = curl_init();

    curl_setopt($my_ch, CURLOPT_HEADER, 0);
    curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, 1); //simple Set curl data to return the simple data instead of all data printing it to the browser.
    curl_setopt($my_ch, CURLOPT_URL, $url);

    $data = curl_exec($my_ch);
    curl_close($my_ch);

    return $data;
}
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Read Also:  Conditional When Case Statement in Laravel 6 Eloquent

Example

I hope you have Got What is How to Post Into Facebook Group with PHP using Graph API And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.