Skip to content

A simple yet powerful PHP library for automating Facebook Page posts. Supports manual posting from local JSON/images as well as AI-powered content generation using Google Gemini. Designed for developers who want a clean, flexible, and cron frndly solution for Facebook automation.

License

Notifications You must be signed in to change notification settings

BotolMehedi/facebook-auto-poster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Facebook Auto Poster

PHP Version License Facebook API AI Powered EmailGitHub

A simple yet powerful PHP library for automating Facebook Page posts. Supports manual posting from local JSON/images as well as AI-powered content generation using Google Gemini. Designed for developers who want a clean, flexible, and cron frndly solution for Facebook automation.

Features β€’ Installation β€’ Usage Examples β€’ Troubleshooting β€’ Contributing


πŸš€ Features

  • πŸ“ Manual posts (JSON + Images)
  • πŸ€– AI-generated content
  • 🎨 AI images with captions
  • πŸ“Έ Only Image posts
  • ✍️ Only Caption posts

πŸ“¦ Requirements

  • PHP 7.4 or higher
  • cURL extension
  • Facebook Access Token
  • Gemini KEY (for AI features)
To use Gemini for AI posting, you must enable billing on your Google Cloud account. Without billing, your API key will not work!

🎨 Installation

I’m not going to tell you how to install it.
If you can’t figure it out yourself, then fcuk uπŸ–•This library is for people who know what they’re doingπŸ˜ͺ


πŸ”‘ Get Facebook Credentials

A. Get Page ID

  1. Go to your Facebook Page
  2. Click "About"
  3. Scroll down to find "Page ID"

B. Get Access Token

  1. Go to Facebook Developers
  2. Create an app (if you don't have one)
  3. Add "Facebook Login" product
  4. Go to Graph API Explorer
  5. Select your app
  6. Generate token with these permissions:
    • pages_show_list
    • pages_read_engagement
    • pages_manage_posts
  7. Click "Generate Access Token"
  8. Copy the token

C. Get Long-Lived Token (Recommended)

Your token expires in 1 hour by default. So get a long-lived token:

<?php
$app_id = 'APP_ID';
$app_secret = 'APP_SECRET';
$short_token = 'ACCESS_TOKEN';

$url = "https://graph.facebook.com/oauth/access_token?" .
       "grant_type=fb_exchange_token&" .
       "client_id={$app_id}&" .
       "client_secret={$app_secret}&" .
       "fb_exchange_token={$short_token}";

$response = file_get_contents($url);
$data = json_decode($response, true);
echo "Long-lived token: " . $data['access_token'];
?>

βš™οΈ Config Setup

<?php

require_once 'facebookAutoPoster.php';

$config = [
    'page_id'            => 'PAGE_ID',
    'page_access_token'  => 'PAGE_ACCESS_TOKEN',
    'image_folder'       => './images/',
    'caption_file'       => './captions.json',
    'ai_use'             => 2, // 1 = AI Mode, 2 = Manual Mode
    'gemini_api_key'     => 'GEMINI_KEY',
    'log_file'           => './logs/facebook_poster.log',
    'facebook_api_version' => 'v20.0'
];

$poster = new FacebookAutoPoster($config);
$result = $poster->post();

echo json_encode($result, JSON_PRETTY_PRINT);

πŸ’‘ Pro Tip: Make sure your images/ and logs/ directories are writable!


πŸ’‘ Usage Examples

1️⃣ Manual Posting

$config['ai_use'] = 2;
$poster = new FacebookAutoPoster($config);

$result = $poster->post();
print_r($result);

πŸ“Œ Note: Place your images in ./images/ and captions in captions.json


2️⃣ AI Image + Caption

$config['ai_use'] = 1;
$ai_poster = new FacebookAutoPoster($config);

$result = $ai_poster->post([
    'post_type' => 'image_caption',
    'prompt'    => 'A beautiful sunset over mountains with vibrant colors'
]);

3️⃣ AI Caption Only

$result = $ai_poster->post([
    'post_type' => 'caption_only',
    'prompt'    => 'Motivational quote about success and perseverance'
]);

4️⃣ AI Image Only

$result = $ai_poster->post([
    'post_type' => 'image_only',
    'prompt'    => 'Abstract digital art with vibrant neon shapes'
]);

⏰ Cron Job Setup

Create cron.php:

<?php
require_once 'facebookAutoPoster.php';

$poster = new FacebookAutoPoster([
    'page_id' => 'PAGE_ID',
    'page_access_token' => 'PAGE_ACCESS_TOKEN',
    'ai_use' => 2,
    'image_folder' => '/images/',
    'caption_file' => '/captions.json'
]);

$result = $poster->post();

file_put_contents('./logs/cron.log',
    date('Y-m-d H:i:s') . ' - ' . json_encode($result) . PHP_EOL,
    FILE_APPEND
);

Cron Schedule Examples:

# πŸ• Every hour
0 * * * * /usr/bin/php /fbauto/cron.php

# πŸ•’ 3 times daily (9 AM, 3 PM, 9 PM)
0 9,15,21 * * * /usr/bin/php /fbauto/cron.php

# πŸŒ™ Once daily at midnight
0 0 * * * /usr/bin/php /fbauto/cron.php

πŸ”§ Utility Methods

// Facebook Token Test
$poster->testFacebookConnection();

// Gemini Key Test
$poster->testGeminiConnection();

// Get Available images
$images = $poster->getAvailableImages();

// Count remaining captions
$count = $poster->getRemainingCaptionsCount();

// Current configuration
$configs = $poster->getConfig();

πŸ“Š Response Format

Every method returns a standardized JSON response:

{
  "status": "success",
  "message": "Post published successfully!",
  "timestamp": "2025-10-03 12:00:00",
  "post_id": "123456789_987654321",
  "post_type": "image_caption"
}

πŸ› Troubleshooting

❌ Invalid Token Error

Problem: Invalid OAuth access token

Solution:

  • Verify your Page Access Token is valid
  • Ensure it has pages_manage_posts permission
  • Regenerate token if expired
πŸ”’ Permission Denied

Problem: Can't write to files or directories

Solution:

chmod 755 images/
chmod 755 logs/
chmod 644 captions.json
⏱️ Rate Limit Exceeded

Problem: Too many requests to Facebook API

Solution:

  • The library retries automatically
  • Reduce posting frequency in cron
  • Wait a few minutes before retrying
πŸ“ No Captions Available

Problem: captions.json is empty or invalid

Solution:

  • Verify JSON format is correct
  • Add more captions to the file
  • Check file permissions

πŸ” Best Practices

  • Store tokens in .env files (never commit them!)
  • Use environment variables for sensitive data
  • Set proper file permissions (755 for dirs, 644 for files)
  • Test in development before production
  • Monitor logs regularly
  • Rotate API keys periodically

πŸ“„ License

This project is licensed under the MIT License - feel free to use, modify, and share! πŸŽ‰


🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request🎯

Found a bug? πŸ› Open an issue


🌟 Star this repo if you find it helpful!

Made with ❀️ and πŸ’¦ by BotolMehedi

About

A simple yet powerful PHP library for automating Facebook Page posts. Supports manual posting from local JSON/images as well as AI-powered content generation using Google Gemini. Designed for developers who want a clean, flexible, and cron frndly solution for Facebook automation.

Topics

Resources

License

Stars

Watchers

Forks

Languages