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
- 📝 Manual posts (JSON + Images)
- 🤖 AI-generated content
- 🎨 AI images with captions
- 📸 Only Image posts
- ✍️ Only Caption posts
- PHP 7.4 or higher
- cURL extension
- Facebook Access Token
- Gemini KEY (for AI features)
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😪
- Go to your Facebook Page
- Click "About"
- Scroll down to find "Page ID"
- Go to Facebook Developers
- Create an app (if you don't have one)
- Add "Facebook Login" product
- Go to Graph API Explorer
- Select your app
- Generate token with these permissions:
pages_show_listpages_read_engagementpages_manage_posts
- Click "Generate Access Token"
- Copy the token
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'];
?><?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/andlogs/directories are writable!
$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
$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'
]);$result = $ai_poster->post([
'post_type' => 'caption_only',
'prompt' => 'Motivational quote about success and perseverance'
]);$result = $ai_poster->post([
'post_type' => 'image_only',
'prompt' => 'Abstract digital art with vibrant neon shapes'
]);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// 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();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"
}❌ Invalid Token Error
Problem: Invalid OAuth access token
Solution:
- Verify your Page Access Token is valid
- Ensure it has
pages_manage_postspermission - 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
- Store tokens in
.envfiles (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
This project is licensed under the MIT License - feel free to use, modify, and share! 🎉
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request🎯
Found a bug? 🐛 Open an issue