Auto post grup facebook api

This script can be used to auto-post to Facebook groups using graph API.

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' => 'http://www.phpgang.com/',
            'picture' => 'http://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.';
        $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;$idata);$i++)
    {
        $dropdown .= "".$groups->data[$i]->name."";
    }
    
    $content = '
    
    #status
    {
        width: 357px;
        height: 28px;
        font-size: 15px;
    }
    
    '.$message.'
    
    Select Group on which you want to post status: '.$dropdown.'
    
    
    ';
}
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;$idata);$i++)
    {
        $dropdown .= "".$groups->data[$i]->name."";
    }
    
    $content = '
    
    #status
    {
        width: 357px;
        height: 28px;
        font-size: 15px;
    }
    
    '.$message.'
    
    Select Group on which you want to post status: '.$dropdown.'
    
    
    ';
}
else
{
    $content = '';
}
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;
}
?>

Is it possible facebook has an ability to auto post a data of an item on my website. Let's say everyday it will post a product automatically in different pages/groups on facebook. Or, alternatively, I could use a button to post the product to different pages/groups in facebook. Is the facebook graph api capable with that?

I built my website using php.

Auto post grup facebook api

kdbanman

9,7559 gold badges42 silver badges76 bronze badges

asked Oct 24, 2015 at 6:43

6

For this, you can use any facebook auto post tool which has scheduled post feature. I can suggest you one from here -Liveurlifehere Tools.

answered Jun 6, 2017 at 2:37

In this guide, I will show you how to use Python and the Facebook Graph API to post to Facebook Groups.


Auto post grup facebook api
Example of Automated Post

This tutorial is very simple and will give you the basics to learn how to use the Facebook API with Python.

If you know nothing about Python, you should start by reading my complete guide on Python for SEO.

Let’s get going, shall we?

What is an API?

An Application Programming Interface, or API, is a method of communication in which applications give access to their data in a structured way, without the need of interacting with the user interface.

Read This Before Posting to Facebook Groups

I created a Facebook group specifically for this tutorial so you can try the API without spamming some random group.

Please, please stick to posting in that Facebook group!

You are allowed to post in that group! Please do.


https://www.facebook.com/groups/744128789503859


Auto post grup facebook api

Let’s look at the steps to post to a Facebook Group using the Graph API.

1. Create a Facebook API App

The first step is to create an App in the Facebook Developers Console and get an access token.

Auto post grup facebook api

If you don’t know how, just check this guide on how to get Facebook Graph API access token.

2. Save Your Credentials

Now, you will need to add your access token to a credentials.json file.

Auto post grup facebook api

3. Read the credentials

Now, let’s create a function to read the credentials file.

import json

def read_creds(filename):
    '''
    Store API credentials in a safe place.
    If you use Git, make sure to add the file to .gitignore
    '''
    with open(filename) as f:
        credentials = json.load(f)
    return credentials

credentials = read_creds('credentials.json')

4. Authorize the App

from facebook import GraphAPI

graph = GraphAPI(access_token=credentials['access_token'])

5. Post Your Message to All Groups

Here add your groups ID.

The group ID can be found in the URL of the Group.

Auto post grup facebook api

message = '''
Read the guide on how to post to Facebook groups using the Facebook API.
'''

link = 'https://www.jcchouinard.com/post-to-groups-using-facebook-graph-api-python/'
groups = ['744128789503859']

for group in groups:
    graph.put_object(group,'feed', message=message,link=link)
    print(graph.get_connections(group, 'feed'))

Full Code

import json

from facebook import GraphAPI

def read_creds(filename):
    '''
    Store API credentials in a safe place.
    If you use Git, make sure to add the file to .gitignore
    '''
    with open(filename) as f:
        credentials = json.load(f)
    return credentials

credentials = read_creds('credentials.json')

graph = GraphAPI(access_token=credentials['access_token'])

message = '''
Add your message here.
'''
link = 'https://www.jcchouinard.com/'
groups = ['744128789503859']

for group in groups:
    graph.put_object(group,'feed', message=message,link=link)
    print(graph.get_connections(group, 'feed'))

This is it!

You now know how to post to Facebook groups using the Graph API and Python.

If you want more Social Media Automation tutorials, be sure to read the complete guides on the Reddit API and the Linkedin API with Python.

Auto post grup facebook api

SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. In a quest to programmatic SEO for large organizations through the use of Python, R and machine learning.

Post navigation